26

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"
2
  • don't see why you can't do that.. ng:show and ng:repeat are not dependant on each other. so you should be able to do it.. give it a try. Commented Jul 29, 2013 at 8:51
  • 1
    I am not sure how to do it as I cannot see an example of something as simple as repeating over a range in the documentation: docs.angularjs.org/api/ng.directive:ngRepeat I wish the documentation was better. Fantastic product but the docs are a real let down for me :-( I guess I am not the only one as I see a huge number of comments and questions at the bottom of the page. Commented Jul 29, 2013 at 9:13

2 Answers 2

26

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

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

Comments

19

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>

1 Comment

omg, I spent half an hour trying to figure out why nothing is displayed and there were no errors in console. do you know why it is not default (track by)?

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.