2

跟进这个问题:AngularJS data bind in ng-bind-html?

如果我要插入的值附加到 ng-repeat 创建的范围怎么办?

myDict = {'Suggestion' : $interpolate('Vote here if you think {{player.name}} is right!')($scope) 
          ...};// $scope is wrongly passed here as 'player' is not a scope variable,

进而

<div ng-repeat="player in players"> <span ng-bind-html="myDict['Suggestion']"></span> </div>

没有自定义指令可以做这样的事情吗?

4

1 回答 1

1

值得庆幸的是,您是决定何时调用该函数的人:

$scope.myDict = function (scope) {
  return {'Suggestion' : $interpolate('Vote here if you think {{player.name}} is right!')(scope);
}

然后在您的 HTML 中:

<div ng-repeat="player in players">
    <span ng-bind-html="myDict({player: player})['Suggestion']"></span>
</div>

这将使用对象字面量为每个玩家调用一次该函数,并将其作为$interpolate调用的“范围”传递。


但是请注意:如果您有数千条记录,则每次调用一个函数可能会有某种性能。打。我能想到的唯一方法是{{players[$index].name}}$interpolate通话中使用...

于 2016-08-20T20:09:11.297 回答