好吧,我有一个喜欢的对象:
$scope.variable = "mike";
$scope.country = "...";
$scope.posts = [
...
{
id:1,
name: 'bla bla bla',
content: 'Hello, my name is {{variable}} and i am from {{country}}'
},
...
];
那么,我如何评估或解析content
属性?
我的想法在我看来是这样用的:
<a ng-click=" variable = 'John' ">Change name here!</a>
<div ng-repeat="post in posts">
<h1>{{post.name}}</h1>
<p>{{post.content}}</p>
</div>
由于
答案 0 :(得分:3)
在控制器中创建一个方法,允许您通过$interpolate
运行字符串,即
// don't forget to inject the $interpolate service
$scope.parseContent = function(template) {
return $interpolate(template)($scope);
};
然后你可以使用
<p>{{parseContent(post.content)}}</p>
答案 1 :(得分:1)
<强>更新强>
假设OP可以访问ES6
。然后尝试使用标准JavaScript API String template
。
查看:强> https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Template_literals
示例:强>
"use strict";
let variable = "mike";
let country = "Australia";
let posts = [
{
id:1,
name: 'bla bla bla',
content: `Hello, my name is ${variable} and i am from ${country}`
}
];
console.log(posts);
&#13;
请注意,字符串的开头和结尾是&#34;`&#34;角色vs&#34;
AngularJS中的示例: