如何使用AngularJS V1.6绑定默认值以使用ng-model选择 [英] how to bind default value to select using ng-model using AngularJS V1.6

查看:66
本文介绍了如何使用AngularJS V1.6绑定默认值以使用ng-model选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<select ng-model="machineSelected"
        ng-options="machine._id as machine.name for machine in machineList"
        ng-change="onChangeMachine(machineSelected)"
        ng-disabled="!mineSelected">
    <!--<option value=""></option>-->
    <option value="">Select</option>
</select>

当我添加

$scope.machineSelected = "";

动态地在控制器中的任何位置,下拉菜单中的选项应设置为选择",但不会更新.

dynamically wherever in the controller, option in drop-down should set as "Select", but it is not updating.

推荐答案

使用nullundefined而不是空字符串作为未选中"值.

Use null or undefined, not an empty string, as the "not selected" value.

文档这样说:

(可选)可以将单个硬编码的<option>元素(其值设置为空字符串)嵌套在<select>元素中.然后,该元素将表示null或未选择"选项.请参见下面的示例进行演示.

Optionally, a single hard-coded <option> element, with the value set to an empty string, can be nested into the <select> element. This element will then represent the null or "not selected" option. See example below for demonstration.

尚不清楚是否不能使用空字符串,但至少他们提到了null.

It's not really clear that an empty string can't be used, but at least they mention null.

这篇关于如何使用AngularJS V1.6绑定默认值以使用ng-model选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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