Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
123
rated 0 times [  128] [ 5]  / answers: 1 / hits: 11475  / 10 Years ago, fri, september 19, 2014, 12:00:00

I'm repeating over an array of objects and I want my variable in the loop to refer to just one property of each object.



Simplified data example:



var data = [
{
'name': {
'first': 'John',
'last': 'Johnson'
}
'age': 45
},
{
'name': {
'first': 'Larry',
'last': 'Wilson'
}
'age': 45
}
]


I could do:



<div ng-repeat=person in data>{{ person.name.first }}</div>



But what I'd prefer to do is to just focus in on the only part of the object I'm using and do something like this:



<div ng-repeat=person.name in data as name>{{ name.first }}</div>



But that doesn't seem to be working -- is this possible currently?


More From » angularjs

 Answers
4

You can do this with ng-init:



<div ng-repeat=person in data ng-init=name=person.name>{{ name.first }}</div>


https://docs.angularjs.org/api/ng/directive/ngInit


[#42400] Thursday, September 18, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jackelyn

Total Points: 303
Total Questions: 103
Total Answers: 102

Location: Turks and Caicos Islands
Member since Sun, Mar 7, 2021
3 Years ago
jackelyn questions
Thu, Apr 8, 21, 00:00, 3 Years ago
Sun, Feb 28, 21, 00:00, 3 Years ago
Mon, May 25, 20, 00:00, 4 Years ago
Thu, Apr 30, 20, 00:00, 4 Years ago
;