选择不使用对象的ng-options作为模型[Angular 1.5]

时间:2017-04-04 06:23:18

标签: javascript angularjs angularjs-ng-options

在代码下方,所有内容都可以正常加载,但不会选择默认选项。

此外,option.groupJID的其中一个下拉项目值与MyCtrl.groupJID = 1655153520848838061@groups.go.to匹配

<select id="groups-list2" ng-model="MyCtrl.groupJID" 
 ng-options="option.groupJID as option.profile.name for option in MyCtrl.userGroups track by option.groupJID">
</select>

请查看

ng-options enter image description here

功能:

SomeNetworkCall.then(function(success){
   MyCtrl.groupJID = success.peer; //'1655153520848838061@groups.go.to'

   MyCtrl.userGroups = success.groups;

});

success.groups:

[
      {
        "groupJID": "11110567@groups.go.to",

        "profile": {
          "name": "Flock Desktop Dev - QA issues",
          "description": "QA/Dev Channel"
        }
      },
      {
        "groupJID": "1655153520848838061@groups.go.to",

        "profile": {
          "name": "Some group name",
          "description": "awesome Channel"
        }
      }
      {
        "groupJID": "1111067890@groups.go.to",

        "profile": {
          "name": "Flock Desktop 1",
          "description": "QA/Dev Channel"
        }
      },
      {
        "groupJID": "1111045678@groups.go.to",

        "profile": {
          "name": "Flock Desktop 2",
          "description": "QA/Dev Channel"
        }
      }
]

1 个答案:

答案 0 :(得分:2)

取自docs

  

在同一表达式中使用select as和track by时要小心。

您在选择为中指定的值将用作选项值,但是当您添加 Track By 表达式时,将跟踪的值是选择值而不是对象。 在您的情况下,ngModel值为option.groupJID,并且track by expression的计算结果为option.groupJID.groupJID,这是未定义的。

要使其正常工作,您可以删除Track by expression

<select id="groups-list2" ng-model="MyCtrl.groupJID" 
    ng-options="option.groupJID as option.profile.name for option in MyCtrl.userGroups">
</select>

Plunker here