10

Does anyone know why in AngularJS, the select selector doesn't work without the ng-model?

<select ng-model="bla_bla" ng-options="obj.value as obj.key for obj in languages"/>
<select ng-options="obj.value as obj.key for obj in languages"/>

The first line of code above will work while the second one will not work. Why ???

Thanks!

1 Answer 1

13

Using the select tag within an Angular app generates the ng-select directive, which is an HTML SELECT element plus angular data-binding. Looking in the Angular source for this select directive, here are the first few lines from its link method:

link: function(scope, element, attr, ctrls) {
    // if ngModel is not defined, we don't need to do anything
    if (!ctrls[1]) return;
    ...

So it is actually built into the framework to abort linking the directive if ngModel is not defined, and so no binding takes place.

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

3 Comments

@VishalKumarSahu - You could just provide ng-model with a dummy variable.
that will save the data
@VishalKumarSahu Obviously. But you don't have to use the data, which is why it's a 'dummy' variable. You should ask a new question if this is unclear to you.

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.