angular select doesn't work without ng-model

妖精的绣舞 提交于 2019-12-08 15:53:25

问题


Does anyone know why in Angular, 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:


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.



来源:https://stackoverflow.com/questions/16644676/angular-select-doesnt-work-without-ng-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!