AngularNewbie
AngularNewbie

Reputation: 83

Angular syntax: how do I get a list from a larger array for ng-repeat? -- Newbie

Beginner question! I've got an array of form

$scope.myArray['a'] = ['abc', 'def', 'etc'];
$scope.myArray['b'] = ['bbc', 'bef', 'btc'];
...

I want to use ng-repeat to present abc def etc

So far, so good. The problem is that I want to use an object obj.something as an index in the ng-repeat. What is the correct syntax? I tried:

<li ng-repeat="x in myArray.obj.something">  
<li ng-repeat="x in myArray(obj.something)">
<li ng-repeat="x in myArray[obj.something]">
<li ng-repeat="x in myArray{obj.something}">
<li ng-repeat="x in someFunction(obj.something)">

And so on. What's the correct syntax or way to do this?

Here's the plunk: http://plnkr.co/edit/3uGOA6G2XisBprnfp2MB?p=preview

Upvotes: 0

Views: 237

Answers (1)

charlietfl
charlietfl

Reputation: 171669

You aren't really working with an array since javascript does not have associative arrays. What you have is an object literal

{    
    a: ['abc', 'def', 'etc'],
    b: ['bbc', 'bef', 'btc']    
}

If you just wanted to loop over a array with ng-repeat:

<li ng-repeat="x in myArray.a">{{x}}</li>

If wanted to loop whole object

<li ng-repeat="(k, arr) in myArray">
      <h3>My Key is: {{k}}</h3>
      <div ng-repeat="item in arr"> {{item}}</div> 
</li>

DEMO

Upvotes: 3

Related Questions