AngularJS select doesn't work without ng-model
Asked Answered
B

1

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!

Beaune answered 20/5, 2013 at 7:33 Comment(0)
A
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.

Amsterdam answered 20/5, 2013 at 8:4 Comment(3)
@VishalKumarSahu - You could just provide ng-model with a dummy variable.Amsterdam
that will save the dataLedeen
@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.Amsterdam

© 2022 - 2024 — McMap. All rights reserved.