有没有办法让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
。
答案 0 :(得分:10)
您可以使用$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>
答案 1 :(得分:5)
您可以在括号内执行JS:
<p>{{Txt + ' ' + Rest}}</p>
或者创建一个返回所需文本的函数:
$scope.getText = function() {
return $scope.Txt + ' ' + $scope.Rest;
}
<p>{{getText()}}</p>
答案 2 :(得分:1)
当您使用javascript模型时,您可以执行以下简单的操作:
var rest = 'and this is the rest of it';
$scope.txt = 'This is some text ' + rest;
在视图中,请保留<p>{{txt}}</p>
或者,您可以将表达式串在一起<p>{{txt}} {{rest}}</p>