AngularJS:使用子对象属性作为ng-model

时间:2013-09-27 05:13:45

标签: c# javascript angularjs

我有一个下拉列表,它绑定到具有以下结构的对象用户:

{
  userId:1,
  userLogin:"test",
  locale: { localeCode:"en-US" }
}

此结构直接来自C#模型类,其中User和Locale是作为关联链接的两个类(User具有一个Locale)。

所以我想定义下面的下拉列表而不做任何自定义逻辑,比如以特殊方式序列化对象,或者将$ watch添加到“UI”属性,这样我就可以在后台更新locale.localeCode。

<select id="dropLocale" required="required" addempty="true" 
        ng-model="user.locale.localeCode" 
        ng-options="i.localeCode as i.localeCode for i in localeList"></select>

任何帮助将不胜感激! 感谢

1 个答案:

答案 0 :(得分:0)

假设数据结构是这样的

$scope.localeList = [{
    userId: 1,
    userLogin: "test",
    locale: {
        localeCode: "en-US"
    }
}, {
    userId: 2,
    userLogin: "test2",
    locale: {
        localeCode: "de"
    }
}];

我认为你错过了locale级别,试试这个

ng-options="i.locale.localeCode as i.locale.localeCode for i in localeList"
相关问题