Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use ng-repeat to iterate over an array in AngularJS?

Tags:

angularjs

I am having some trouble with using AngularJS ng-repeat. Here is what I have:

<div ng:show="selected == 3">     <div class="columns">         <textarea rows="4" data-ng-model="modal.data.answer[1].text" cols="91">1</textarea>     </div>     <div class="columns">         <label>Explanation</label>         <textarea rows="2" data-ng-model="modal.data.answer[1].explanation" cols="91"></textarea>     </div>     <div class="columns">         <div class="colx2-left">             <span class="label">Correct</span>             <input type="checkbox" data-ng-model="modal.data.answer[1].correct" class="check-box"><input type="hidden" value="false" name="Details[0].Correct">         </div>         <div class="colx2-right">             <label>Image File</label>             <input type="text" data-ng-model="modal.data.answer[1]image" class="full-width">         </div>     </div> </div> 

The modal.data.answer array will always have six elements. Can I use ng-repeat to create the HTML for these and have it repeat even the [x] numbers in the array and have it increment the number on the first line for "selected =" from 3 to 8 ?

As far as I know I can use <div ng-repeat="t in [3,4,5,6,7,8]"> </div> but how can I get the values of t into things like:

data-ng-model="modal.data.answer[1].text" 
like image 723
Alan2 Avatar asked Jul 29 '13 08:07

Alan2


People also ask

What is the use of NG-repeat in AngularJS?

AngularJS ng-repeat Directive The ng-repeat directive repeats a set of HTML, a given number of times. The set of HTML will be repeated once per item in a collection. The collection must be an array or an object.

What can I use instead of NG-repeat?

But ng-repeat is not the right thing to use when you have large datasets as it involves heavy DOM manipulations. And you should consider using ng-repeat with pagination. You can consider using transclusion inside a custom directive, to achieve the behavior you are looking for without using ng-repeat.

What is difference between NgFor and Ng-repeat?

ng-repeat created inherited child scope for each element of collection, while *ngFor creates local variable in the that block.

Why is NG-repeat not working?

Solution 1 There are two mistakes in your code: In your table, you have to wrap the properties between {{ and }}, for example {{employee. Fname}} instead of just employee. Fname .


2 Answers

your edit is heading in the right way, you can iterate through a list of integers easily, the you just use the value of you index like that

<div ng-repeat="t in [3,4,5,6,7,8]"> .... data-ng-model="modal.data.answer[t].text" ... 

I made a fiddle if you want to try http://jsfiddle.net/DotDotDot/tweyS/ (the input field is just here to change the selected value, as in your code sample, in order to display the right fields)

have fun

like image 104
DotDotDot Avatar answered Sep 21 '22 20:09

DotDotDot


Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys.

  <div ng-repeat="n in [42, 42, 43, 43] track by $index">      {{n}}   </div> 
like image 31
Eassa Nassar Avatar answered Sep 21 '22 20:09

Eassa Nassar