Ano*_*per 7 javascript angularjs angularjs-scope
有没有办法让AngularJS评估模型数据中的表达式?
HTML:
<p>
{{Txt}}
</p>
Run Code Online (Sandbox Code Playgroud)
模型:
{
Txt: "This is some text {{Rest}}"
}
{
Rest: "and this is the rest of it."
}
Run Code Online (Sandbox Code Playgroud)
最终结果将是:This is some text and this is the rest of it.
Ant*_*Chu 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);
};
}
Run Code Online (Sandbox Code Playgroud)
<div ng-app ng-controller="ctrl">
<input ng-model="Txt" size="60" />
<p>{{ Txt }}</p>
<p>{{ interpolate(Txt) }}</p>
</div>
Run Code Online (Sandbox Code Playgroud)
您可以在括号内执行JS:
<p>{{Txt + ' ' + Rest}}</p>
Run Code Online (Sandbox Code Playgroud)
或者创建一个返回所需文本的函数:
$scope.getText = function() {
return $scope.Txt + ' ' + $scope.Rest;
}
<p>{{getText()}}</p>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4447 次 |
| 最近记录: |