【问题标题】:$interpolate values from a ng-repeat created scope$interpolate 来自 ng-repeat 创建范围的值
【发布时间】:2016-12-26 16:31:27
【问题描述】:

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

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

【问题讨论】:

    标签: angularjs angularjs-directive angularjs-ng-repeat ng-bind-html angularjs-interpolate


    【解决方案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 调用的“范围”传递。


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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 2012-09-03
      相关资源
      最近更新 更多