2015-05-25 29 views
0

在这种codepen我attemptling从范围变量加载选项元素填充所有选项:不受NG-选项

<html> 

<head> 
    <title>AngularJS test</title> 
</head> 

<body ng-app ng-controller="TestCtrl"> 


    <select ng-init="vendor = vendors[0]" ng-model="vendor" ng-options="vendorName as vendor.name for vendorName in vendors"></select> 

    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.4/angular.min.js"></script> 

    <script src="http://code.jquery.com/jquery-1.11.3.min.js"></script> 

</body> 

</html> 


function TestCtrl($scope) { 

    $scope.vendors = [{ 
    name: 'Oracle1', 
    name: 'Oracle2' 
    }]; 

} 

但是,仅仅在位置[1]该元件正被加载。

CodePen:http://codepen.io/anon/pen/OVbXJV?editors=101

为什么只是1选项元素加载?

回答

2

您应该有一个对象数组,而不只是一个包含多个名称的对象。

$scope.vendors = [ 
    {name: 'Oracle1'}, 
    {name: 'Oracle2'} 
]; 

然后可以使用ng-options像这样:

ng-options="vendor.name for vendor in vendors"