【问题标题】:Ternary not working properly for function call in angular expressions三元在角度表达式中的函数调用不能正常工作
【发布时间】:2014-12-16 12:13:22
【问题描述】:

如果我使用,我必须在 ng-repeat 中调用 $last 上的函数

   div(ng-repeat="note in notes")
    li: {{note}}
    {{$last ? 'true' : 'false' }}

它只在最后一个 li 元素上打印 false,但如果我调用一个函数而不是 'true',则结果相同,如下所示

   div(ng-repeat="note in notes")
    li: {{note}}
    {{$last ? update() : 'false' }}

每个循环都会调用该函数。

为了您的信息,我尝试了link中给出的“&&”类型的解决方案

{{$last && update() || '' }}

也可以,但不起作用。

【问题讨论】:

    标签: javascript angularjs angularjs-ng-repeat


    【解决方案1】:

    您没有做任何明显的错误,因此错误可能在您的代码中的其他地方。请参阅以下内容:

    app.controller('MyCtrl', function ($scope) {
      $scope.notes = ['Note 1','Note 2'];
      $scope.update = function () {
        return 'i haz updated!1';
      };
    });
    

    <div ng-controller="MyCtrl">
      <ul ng-repeat="note in notes">
        <li>
          {{note}} - isLast:{{$last ? 'true' : 'false'}} - {{$last ? update() : '???'}}
        </li>
      </ul>
    </div>
    


    笨蛋http://plnkr.co/edit/DwCpcC

    【讨论】:

      【解决方案2】:

      请查看demo

      <ul>
          <li data-ng-repeat="note in notes">
             {{ note }}
             {{ $last ? update():''}}
           </li>
      </ul>
      

      【讨论】:

      • 当我们有$first$last 时,为什么还要$index?除此之外,这个答案有什么不同?
      猜你喜欢
      • 1970-01-01
      • 2019-03-09
      • 1970-01-01
      • 2020-06-07
      • 2021-04-06
      • 1970-01-01
      • 1970-01-01
      • 2021-07-20
      • 1970-01-01
      相关资源
      最近更新 更多