2016-01-29 64 views
1

这里我有一个select,我想在这里重复供应商的联系。Ng-数组中对象的选项

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="app" ng-controller="AppCtrl"> 
 
    Hello {{name}} 
 
    
 
    <div ng-repeat="supplier in suppliers"> 
 
     {{supplier.name}} 
 
     
 
     <select ng-options="contact.id as contact.name for contact in supplier.contacts"> 
 
     
 
     </select> 
 
    </div> 
 
    
 
</div> 
 
<script> 
 
var app = angular.module('app',[]); 
 
    
 
    //app.directive('appDirective', function() {}); 
 
    //app.factory('appService', function() {}); 
 
    
 
    app.controller('AppCtrl', ['$scope', function($scope){ 
 
     $scope.name = "dave"; 
 
     
 
     $scope.suppliers = [ 
 
     \t \t {id: 0, name: "dave", contacts : [ 
 
      \t {id: 0, name: 'c1'}, 
 
       {id: 1, name: 'c2'}, 
 
       {id: 2, name: 'c3'}, 
 
       {id: 3, name: 'c4'}, 
 
      ]}, 
 
      {id: 1, name: "Steve", contacts : [ 
 
      \t {id: 0, name: 'c1'}, 
 
       {id: 1, name: 'c2'}, 
 
       {id: 2, name: 'c3'}, 
 
       {id: 3, name: 'c4'}, 
 
      ]} 
 
     ] 
 
    }]); 
 

 
</script>

这里我有控制器。

这是怎么回事?

http://jsfiddle.net/gnosticdave/091vpadb/1/

回答

2

contacts是每个supplier的一部分 - 所以你的数组实际上是supplier.contacts

select ng-options="contact.id as contact.name for contact in supplier.contacts" 
                  ^^^^^^^^^^^^^^^^^^^^ 

此外,ngOptions需要一个ngModel

演示:http://jsfiddle.net/091vpadb/3/

+0

伍模型固定它。谢谢 – Dmac

0

变化:

<select ng-options="contact.id as contact.name for contact in contacts"> 

到:

<select ng-options="contact.id as contact.name for contact in supplier.contacts"> 
+0

对不起,我忘了把它放在我的代码中,但即使用它,它仍然不工作。 – Dmac