1

I have seen a lot of examples to bind array of Objects.

But, all I have is this

years = [1900,1901,1902];

and i want to bind this to the options for my select control. I have this template:

<select id="carYear" required>
        <option value="">Select year</option>
        <option ngFor="year in years">{{year}}</option>
</select>

But, it does not work.

I also tried ng-repeat. Any ideas what is wrong here?

2 Answers 2

1

Fiddle here: https://jsfiddle.net/frishi/bzbbo5da/

Basically, you can use a flat array and enumerate it using a <select> The only additional thing you need to do is

<select ng-model="myYears" ng-options="o as o for o in years"></select>

When you use a flat array, you have to tell angular what to use as the key. Angular will do it for you if you use an array of objects.

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

Comments

1

You're missing an asterisk on the ngFor directive and the let keyword.

Try:

<select id="carYear" required>
    <option value="">Select year</option>
    <option *ngFor="let year in years">{{year}}</option>
</select>

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.