[英]AngularJS Expression in Expression
Is there a way to have AngularJS evaluate an expression within model data? 有没有办法让AngularJS评估模型数据中的表达式?
HTML: HTML:
<p>
{{Txt}}
</p>
Model: 模型:
{
Txt: "This is some text {{Rest}}"
}
{
Rest: "and this is the rest of it."
}
The end result would be: This is some text and this is the rest of it
. 最终的结果是:
This is some text and this is the rest of it
。
You can use the $interpolate
service to interpolate the string... 您可以使用
$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>
You can execute JS within the brackets: 您可以在括号内执行JS:
<p>{{Txt + ' ' + Rest}}</p>
Or create a function that returns the text you want: 或者创建一个返回所需文本的函数:
$scope.getText = function() {
return $scope.Txt + ' ' + $scope.Rest;
}
<p>{{getText()}}</p>
As you are using javascript for the model you can do something simple like this: 当您使用javascript模型时,您可以执行以下简单操作:
var rest = 'and this is the rest of it';
$scope.txt = 'This is some text ' + rest;
And in the view, keep your <p>{{txt}}</p>
在视图中,请保留
<p>{{txt}}</p>
Alternatively you can string the expressions together <p>{{txt}} {{rest}}</p>
或者,您可以将表达式串在一起
<p>{{txt}} {{rest}}</p>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.