1

In My AngularJs app, I have an ng-repeat like:

<div data-ng-repeat="pl in parentlist">
  <div id="test{{$index}}" ng-model='list{{$index}}' >Test</div>
</div>

The Id in the Div gets appends and renders like expected id="test0" but the ng-model doesn't behave in the same way and it throws an error

[$parse:syntax] Syntax Error: Token '$index' is unexpected, expecting [:] at column 8 of the expression [list{{$index}}] starting at [$index}}]

How do I achieve this appending? use filters?

5
  • 1
    what is the point of this, you have already array in your hand why do you need these primitive models? Commented Apr 3, 2014 at 17:29
  • I need to dynamically add a div everytime a object is added to the "parentlist". lets say $scope.parentlist.test1=[]; test1 gets added to $scope.parentlist, I want a div to add dynamically with the ng-model="test1". Commented Apr 3, 2014 at 17:33
  • so you need to key of parentlist object am I right? Commented Apr 3, 2014 at 17:44
  • @wickY26 - Thanks,Got what you are trying to say. fixed it. Commented Apr 3, 2014 at 18:10
  • I tried to create an exmaple for you check my answer if it fits you... Commented Apr 3, 2014 at 18:22

3 Answers 3

1

if you need bind your div with the keys of your parentlist just change your ng-repeat to this,

<div data-ng-repeat="(key, value) in parentlist">
   <div id="{{key}}" ng-model="key">{{key}}</div>
</div>

here is working PLUNKER...

Sign up to request clarification or add additional context in comments.

Comments

0

Make list an array. Instead of having individual list variables, you have one array with multiple values

The HTML then becomes

<div id="test{{$index}}" ng-model='list[$index]' >Test</div>

1 Comment

This renders as is like ng-model="list[$index]"
0

If you need to append the index to a string (where you currently are on the ng-repeat loop iteration) you can do it like this:

<div id="num_{{$index}}" ng-repeat="obj in someArray">
    num_{{$index}}
</div>

DEMO: http://plnkr.co/edit/ba6EM0KShiZ8AS8uKVCm

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.