0

What I'm trying to achieve is a simple iteration with ng-repeat over my nested object which looks something like this

$scope.data = {
  name: "",
  id: "",
  nestedObj: {
     prop1: "string",
     prop2: false,
     prop3: ""
  }
}; 

In my view I try it like this

<div ng-repeat="(key, value) in data.nestedObj">
	{{key}} : {{value}}
</div>

...but I constantly get following console error:

Error: Duplicates in a repeater are not allowed. Repeater: (key, value) in data.type key: string

Am I missing out something or what am I doing wrong?

UPDATE!

So after the answer from Sajeetharan and the comment from Slava Utesinov I've set up a Plunkr to give it a try. Surprisingly the answer is, that you obviously can't iterate over an object if it has more than one "empty" key (empty string as value) inside of it.

UPDATE 2

Also it seems, that you can't use the same string value twice ... god I love Angular :)

Finale UPDATE

As Sajeetharan and Slava Utesinov mentioned, track by $index solved it for me

6
  • You have two properties with same prop2 name. Commented Feb 2, 2018 at 13:22
  • My bad - thank you, I updated the question. But this isn't the problem Commented Feb 2, 2018 at 13:30
  • You should reproduce your error via plunker. May be nestedObj is array? Commented Feb 2, 2018 at 13:39
  • So modify to this ng-repeat="(key, value) in data.nestedObj track by $index" Commented Feb 2, 2018 at 15:46
  • I'm going to give it a try and update my question with the result! Commented Feb 3, 2018 at 20:36

1 Answer 1

0

There's a missing bracket in your object.

Try this:

$scope.data = {
  name: "",
  id: "",
  nestedObj: {
     prop1: "string",
     prop2: false,
     prop2: ""
  } 
};
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.