[英]Interpolation inside ng-repeat
I have a markup like this 我有这样的标记
<li ng-repeat="macro in macros" style="cursor:pointer" ng-click="copyToChat($event); $hide()">
{{macro}}
</li>
var macros =
[
"Hey {{activeUserName}} ! I'm sorry"
];
Here {{activeUserName}} is not interpolated. 此处{{activeUserName}}未插值。 How can I resolve it 我该如何解决
Try out this way 尝试这种方式
use a directive to compile your stuff from the string. 使用指令从字符串中编译您的东西。
html HTML
<div ng-app='myApp' ng-controller="Controller">
<li ng-repeat="macro in macros" style="cursor:pointer" ng-click="copyToChat($event); $hide()">
<span compile="macro"></span>
</li>
</div>
script 脚本
var app = angular.module('myApp', []);
app.controller('Controller', function ($scope) {
$scope.macros = ["Hey {{activeUserName}} ! I'm sorry"];
$scope.activeUserName = "maddhur";
})
app.directive('compile', ['$compile', function ($compile) {
return function (scope, element, attrs) {
scope.$watch(function (scope) {
return scope.$eval(attrs.compile);
},function (value) {
element.html(value);
$compile(element.contents())(scope);
});
};
}])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.