最初的NG-模型值在选择不设置 [英] Initial ng-model value not set in select

查看:92
本文介绍了最初的NG-模型值在选择不设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个枚举(使用打字稿I code):

I have an enum (I code using TypeScript):

export enum AddressType
{
    NotSet = 0,

    Home = 1,
    Work = 2,
    Headquarters = 3,

    Custom = -1,
}

然后在我的控制器我有一个名为类型字段,到我设置应在选择输入(我将其设置为AddressType.Headquarters)。

Then in my controller I have a field named type, into which I set the initial value that should be selected in the select input (I set it to AddressType.Headquarters).

最后,在我的HTML我把以下内容:

Finally, in my HTML I put the following:

<select ng-model="Ctrl.type" ng-options="addressType for addressType in Ctrl.getAddressTypes()"></select>

一切似乎除了一件事做工精细:由于某种原因,没有角不选择所有绑定都被更新后,选择3(总部)开始。角创建这样一个额外的选项,而不是:

Everything seems to work fine except one thing: for some reason Angular does does not select "3" (Headquarters) initially in the select after all bindings have been updated. Angular creates an extra option like this instead:

<option value="?" selected="selected"></option>

所以,出于某种原因角想不通最初选择在组合选择。

So for some reason Angular cannot figure the initial option to select in the combo.

如果用户选择的组合框的其他选项,Ctrl.type被适当更新,以便精细该部分的结合的作品。基本上我的问题是,只是没有选择应最初选择的选项符合市场预期。

If the user selects another option of the combo box, Ctrl.type is updated properly so the binding works fine for that part. Basically my problem is just that the option that should be selected initially is not selected as expected.

我是什么在这里失踪,是造成这个问题?

What am I missing here that is causing that problem?

推荐答案

发现了问题:

由Ctrl.getAddressTypes()返回的数组是一个字符串数组:

The array returned by Ctrl.getAddressTypes() was an array of strings:

["0", "1", "2", "3", "1"]

和什么被存储在Ctrl.type是型号。

and what was stored in Ctrl.type was of type number.

AngularJS提供给NG-选项提供给使用===操作符NG-模型的值的数组进行比较。 3不等于3在这种情况下 - 这就是为什么它没有工作。

AngularJS compares the array supplied to ng-options to the value supplied to ng-model using the '===' operator. 3 does not equal to "3" in that case - that's why it did not work.

这篇关于最初的NG-模型值在选择不设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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