具有多个字段的AngularJS ng-options

时间:2012-11-28 09:56:13

标签: javascript html angularjs

我有一组带有用户信息的对象。

"Id": "1",
"Firstname": "ABCDEF",
"Lastname": "GHIJK",
"Middlename": ""

在我的选择选项中,我想显示两个字段 - Firstname Lastname。我不明白该怎么做以及如何将它绑定到ng-model

5 个答案:

答案 0 :(得分:94)

你可以试试这个:

<select 
  name="users" 
  ng-model="selectedUser" 
  ng-options="user.Id as user.Firstname + ' ' + user.Lastname for user in users">
</select>

文档中的更多信息:https://docs.angularjs.org/api/ng/directive/select

答案 1 :(得分:12)

将数组对象的格式视为这样。

$scope.userInfo = [
                    {"Id": "1", "Firstname": "ACDEF", "Lastname": "GHIJK", "Middlename": ""},
                    {"Id": "2", "Firstname": "BADEF", "Lastname": "HIGJK", "Middlename": ""},
                    {"Id": "3", "Firstname": "CDBEF", "Lastname": "IIHJK", "Middlename": ""},
                ]

您可以显示两个这样的字段,并将其绑定到模型,如下所示。

<select ng-model="userInfo" ng-options="s.Firstname +' '+ s.Lastname for s in userInfo" class="span2"> </select> 

答案 2 :(得分:3)

只是一个额外的更新,

我必须在括号中显示姓氏,所以这里是我如何修改答案并使其正常工作。

<select 
   name="users" 
   ng-model="selectedUser" 
   ng-options="user.Id as user.Firstname + ' (' + user.Lastname + ') ' for user in users">
</select>

答案 3 :(得分:0)

Theres始终处于非ng-options方案中,您可以在ng-repeat项中绑定两个参数:

  <select name="user" ng-model="vm.asignedUser">
    <option value="">Select user</option> 
    <option value="{{ {'name':user.name, 'id': user.id} }}" ng-repeat="user in vm.users">
      {{ user.nombreLargo }}
    </option>
  </select>

答案 4 :(得分:0)

这对我有用:

<select ng-model="color" ng-options="(c.name+' '+c.shade) for c in colors"></select>

来源:https://groups.google.com/d/msg/angular/ljJd5x1Tgrk/bZuVWCA1hO4J