一尘不染

表达式中的AngularJS表达式

angularjs

有没有办法让AngularJS在模型数据中评估表达式?

HTML:

<p>
{{Txt}}
</p>

模型:

    { 
     Txt: "This is some text {{Rest}}"
    }

    {
      Rest: "and this is the rest of it."
    }

最终结果将是:This is some text and this is the rest of it


阅读 236

收藏
2020-07-04

共1个答案

一尘不染

您可以使用该$interpolate服务来插值字符串…

function ctrl ($scope, $interpolate) {
    $scope.Txt = "This is some text {{Rest}}";
    $scope.Rest = "and this is the rest of it.";
    $scope.interpolate = function (value) {
        return $interpolate(value)($scope);
    };
}

<div ng-app ng-controller="ctrl">
    <input ng-model="Txt" size="60" />
    <p>{{ Txt }}</p>
    <p>{{ interpolate(Txt) }}</p>
</div>

JSFiddle

2020-07-04