0

I have a variable "options" that contains a list ['a', 'b', 'c'], and like this is displayed on the <h3> below.

Now I want to create a button for each element in the list, with the index appended to the id and as the value, and displaying the text in the button.

This is my code, but however I only get an empty button. What's wrong?

<h3>{{options}}</h3>
<form id="choice" name='form' method="POST" action="/" ng-repeat="i, option in options">
    <button id="button{{i}}" type="submit" value="{{i}}">{{option}}</button>
</form>

EDIT: I made my code easier and it still doesn't show any of the 3 options in the <span> but it shows properly the list in the <h3>. Is it perhaps something that I need to install in order to run ng-repeat? I have the angular.min.js in the <head>

<h3>{{options}}</h3>
<form id="choice" name='form' method="POST" action="/" ng-repeat="option in options">
    <span>{{option}}</span>
</form>

I have this in the header:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
3
  • Did you try grouping (i, option) in parentheses? Commented Nov 18, 2016 at 17:11
  • I just did and still fails. I edited my question with an easier example that still fails. Commented Nov 18, 2016 at 17:42
  • Any errors in the console? Commented Nov 18, 2016 at 20:20

2 Answers 2

0

It seems it's just the missing brackets. Should be ng-repeat="(i, option) in options" in your case.

https://docs.angularjs.org/api/ng/directive/ngRepeat

UPD Also ng-repeat should be an attribute of <button> in the first example and of <span> in the second one. Unless you want 3 forms on the page.

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

2 Comments

Seems that it still fails. I edited my question with some further info
Oh I see. You need to move ng-repeat to <span> tag. Angular makes multiple instances of a tag ng-repeat is attribute of. @Arturo
0

Just make sure you have the correct list in the options array and then try using it like

<h3>{{options}}</h3>
<form id="choice" name='form' method="POST" action="/" ng-repeat="option in options">
     <button id="button{{$index}}" type="submit">{{option}}</button>
</form>

Comments

Your Answer

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