0

ng-repeat nested array's last element to be assigned to a $scope.variable

<tr ng-repeat="user in vm.users | orderBy:sortType:sortReverse | filter:searchTool">
<td >{{user.name}}</td>
<td ><div ng-repeat="item in user.feed track by $index"><span ng-show="$last">{{item.status}}  </span></div></td>

I want this last {{item.status}} to be used for a function... for example

signal({{item.status | $last }})
Riyesh
  • 63
  • 10
  • You could write a directive to detect ng-repeat render and fire the function on last element as shown in this [answer](http://stackoverflow.com/a/13472605/3543808) – Gangadhar Jannu Feb 25 '17 at 17:32
  • Possible duplicate of [ng-repeat finish event](http://stackoverflow.com/questions/13471129/ng-repeat-finish-event) – Gangadhar Jannu Feb 25 '17 at 17:32

1 Answers1

0

First, iterating through an array just to display its last element isn't really the best thing to do. Why not just display the last item directly?

<div>
  <span ng-if="user.feed.length">{{ user.feed[user.feed.length - 1].status }}</span>
</div>

Now, to use it in a function, well, just use it:

<span ng-if="user.feed.length" ng-click="signal(user.feed[user.feed.length - 1])">{{ user.feed[user.feed.length - 1].status }}</span>
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255