三元在角度表达式中不能正常工作

时间:2021-09-13 21:20:12

I have to call a function on $last in ng-repeat, if I use,

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

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

It prints false only on last li element, but for the same if I call a function instead of 'true', like below

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

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

the function is called for each loop.

为每个循环调用该函数。

For your information, I had tried "&&" kind of solution given in this link

为了您的信息,我在这个链接中尝试了“&&”类解决方案

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

also, but not working.

也,但没有工作。

2 个解决方案

#1


1  

You are not doing anything obvious wrong so maybe error is somewhere else in your code. See the following:

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

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>

三元在角度表达式中不能正常工作


Plunker http://plnkr.co/edit/DwCpcC

#2


1  

Please check the demo

请查看演示

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

#1


1  

You are not doing anything obvious wrong so maybe error is somewhere else in your code. See the following:

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

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>

三元在角度表达式中不能正常工作


Plunker http://plnkr.co/edit/DwCpcC

#2


1  

Please check the demo

请查看演示

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