Reputation: 1794
I need to be able to access the current element of ng-repeat inside a directive. See the jsfiddle for this: http://jsfiddle.net/terebentina/TVEnN/
I am expecting a hash in the console (like {width: 100, height: 100}) but I am not really sure how to do that.
Any help would be much appreciated.
Upvotes: 7
Views: 21637
Reputation: 821
In addition to @ganaraj's answer:
There's a pitfall when using filters. By using filters, e.g. to order items alphabetically, the $index
resp. the key
variable does no longer represent the actual position of the item in the items array, but its current position in the ng-repeat
-block.
See this link
Upvotes: 0
Reputation: 26841
Here is a fiddle that works ( and outputs what you are expecting).
Things to Note about ng-repeat
:
ng-repeat
always creates a NEW SCOPE for each element in the array / object. ng-repeat
. For ex: if you defined ng-repeat = "page in pages"
then the current element is available in the scope as page. If you defined ng-repeat = "(key,value) in object"
both key
and value
are available in this new scope.$index
.ng-repeat
creates a new scope and all scopes inherit from their parent the pages
in the above example is still available in this new scope, which means you could do stuff like $scope.pages.length
if you need to.Upvotes: 14