$从 ng-repeat 创建的范围内插入值

$interpolate values from a ng-repeat created scope

跟进这个问题: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>

是否可以在没有自定义指令的情况下做这样的事情?

谢天谢地,您是决定何时调用该函数的人:

$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>

这将使用对象文字为每个玩家调用一次该函数,并将其作为 "scope" 传递给 $interpolate 调用。


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