2013-12-09 68 views
1

我有这个数组在我范围AngularJS NG选项属性值标题

$scope.containers = [{id: 1, title: "Icebox"}, {id: 2, title: "Summer"}, 
{id: 3, title: "Milestone"}]; 

我可以建立这样的NG-选择?

<option value="1">Icebox</option> 
<option value="2">Summer</option> 
<option value="3">Milestone</option> 
+0

它把指数,而不是v.id – techvineet

回答

3

是的,如果你感兴趣的显示标题用这样的方式:

<select> 
    <option 
    ng-repeat="v in containers" 
    value="{{v.id}}" 
    title="{{v.title}}" 
    ng-selected="adresses.indexOf(v) == 0">{{v.title}} 
    </option> 
</select> 

演示Fiddle

但是如果你想只显示valueng-options将enogh

+1

我也要用NG选项办呢? – techvineet

+0

如果你想显示标题和价值,使用上述方式 –

+0

因为我知道你不能用'ng-options'显示标题 –

6

您可以使用ng-options

<select ng-options="v.id as v.title for v in containers" ng-model="selectedId">   
</select> 

Fiddle DEMO

+1

这不会在选项上设置标题属性... – epeleg

+2

OP没有提及瓷砖属性。他只关心他如何以对象的形式显示标题。所以这是正确的答案。 “... ... ... ... ... ...在...中”建设是一条路。 –

0

我跑在这个同样的问题,我在那里需要使用NG选项,这是我的解决方案...

HTML:

<select ng-options="item.Id as item.Name for item in vm.listOfItems"> 
    <option disabled>Select a value</option> 
</select> 

控制器:

vm.$onInit = function() { 
    $timeout(function() { 
     $("option").each(function (index, element) { 
      var text = $(element).text(); 
      $(element).attr("title", text); 
     }); 
    }); 
}