Angular.js get index of last element in 2d array in ng-repeat

I have this 2d array in my controller:

1 2 6 1 2 7 9 2 1 5 3 2 6 

I need to show the user the last element of each row (example: 6, 9, 2, 6) I think I need to use some combination of the ng-repeat directive or get the index of the $ last array using angular filters .

Here is what I have but not working:

 <div ng-repeat="row in array"> {{row[$last].property}} </div> 

thanks

+4
source share
2 answers

Example: http://jsfiddle.net/TheSharpieOne/cLHcU/
Note that in the newer version of angular, you need to register the controller instead of throwing it into the global window object. Here is an example using 1.4: http://jsfiddle.net/TheSharpieOne/cLHcU/42/ (note: the repetition remains the same).

HTML / Template: (All work is done here) There is no need for nested repetition.

 <span ng-repeat="arr in myArr"> {{arr[arr.length-1]}} </span> 

Controller: (only has an array)

 function myCtrl($scope) { $scope.myArr = [[1,2,6],[1,2,7,9],[2],[1,5,3,2,6]]; } 
+2
source

As mentioned in ng-repeat documentation $last is the boolean value, not the index:

true if the repeated element is the last in the iterator.

So the correct syntax

 {{row[row.length-1].property}} 
+2
source

Source: https://habr.com/ru/post/1501936/


All Articles