没有ng-model,AngularJS选择不起作用 [英] AngularJS select doesn't work without ng-model
问题描述
有人知道为什么在AngularJS中没有ng-model
时select
选择器不起作用吗?
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 ???
谢谢!
推荐答案
在Angular应用程序中使用select
标记会生成ng-select
指令,该指令是HTML SELECT
元素加上角度数据绑定.在Angular源代码中查找此select
指令,以下是其link
方法的前几行:
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;
...
因此,如果未定义ngModel
,则它实际上是内置在框架中以中止链接directive
的操作,因此不会发生绑定.
So it is actually built into the framework to abort linking the directive
if ngModel
is not defined, and so no binding takes place.
这篇关于没有ng-model,AngularJS选择不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!