没有ng-model,AngularJS选择不起作用 [英] AngularJS select doesn't work without ng-model

查看:123
本文介绍了没有ng-model,AngularJS选择不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道为什么在AngularJS中没有ng-modelselect选择器不起作用吗?

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆