Angular ng-options按问题跟踪

时间:2015-07-01 13:43:15

标签: angularjs

我有一个使用ng-options显示的对象数据集。我使用track by

将对象ID值绑定到值

目前,正在包含数据值,但它们以逗号显示。例如......

$scope.items = [
   {ID: '2012', Title: 'Chicago'},
   {ID: '2013', Title: 'New York'},
   {ID: '2014', Title: 'Washington'},
];

<select ng-options="item.Title for item in items track by item.ID">
</select>

但这会呈现......

<option value="2,0,1,2" label="Chicago">Chicago</option>
<option value="2,0,1,3" label="New York">New York</option>

为什么要添加这些逗号,如何删除它们?

6 个答案:

答案 0 :(得分:9)

您不需要跟踪:

<select ng-options="i.ID as i.Title for i in items" ng-model="someModel"></select>

渲染后你将拥有:

<option value="2012">Chicago</option>
<option value="2013">New York</option>

答案 1 :(得分:1)

你想:

{{view "select" prompt="All products"
       content=model.category.products
       optionLabelPath="content.name"
       optionValuePath="content.name"
       value=selectedProduct
       class="form-control"}}

跟踪只是通过数组排序帮助内部角度。请参阅:stack overflow answer

答案 2 :(得分:1)

试试这个:

<select ng-model="selectedItemID" ng-options="item.ID as item.Title for item in items">
</select>

Selected ID: {{selectedItemID}}

答案 3 :(得分:1)

您的方式

<select ng-options="item.ID as item.Title for item in items" ng-model="someModel"></select>

Fiddle

替代方式

<select ng-model="selectedItem">
        <option ng-repeat="item in items" value="{{item.ID}}">{{item.Title}}</option>
</select>

Fiddle2

答案 4 :(得分:1)

&#13;
&#13;
function MyController($scope){
  $scope.items = [
		                {ID: '2012', Title: 'Chicago'},
		                {ID: '2013', Title: 'New York'},
		                {ID: '2014', Title: 'Washington'},
		             ];
  };
&#13;
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html ng-app>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
</head>

<body ng-controller="MyController">		
		<select ng-model="selectedItem" ng-options="item.ID as item.Title for item in items track by item.ID"></select>
		<br/>{{selectedItem}}
  </body>
  </html>
&#13;
&#13;
&#13;

以下应该工作: -

  <select ng-model="selectedItem" ng-options="item.ID as item.Title for item in items track by item.ID"></select>

  {{selectedItem}}

答案 5 :(得分:0)

如果你希望它是id,那么你应该使用

item.id作为项目

中项目的item.name