1

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

   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

   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.

Community
  • 1
  • 1
Kamalakannan J
  • 2,818
  • 3
  • 23
  • 51

2 Answers2

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>

imgur


Plunker http://plnkr.co/edit/DwCpcC
Mikko Viitala
  • 8,344
  • 4
  • 37
  • 62
1

Please check the demo

<ul>
    <li data-ng-repeat="note in notes">
       {{ note }}
       {{ $last ? update():''}}
     </li>
</ul>
Asik
  • 7,967
  • 4
  • 28
  • 34