2016-07-27 60 views
1

我想從控制器中的下拉列表中顯示列表。該陣列是控制器變量,如下所定義:ng-options和控制器數組

(function(angular) { 
    'use strict'; 
angular.module('staticSelect', []) 
    .controller('ExampleController', ['$scope', function($scope) { 
    self = this; 
    self.options = [ 
     {id: 1, label: "Item 1"}, 
     {id: 2, label: "Item 2"}, 
     {id: 3, label: "Item 3"} 
     ]; 

    $scope.data = { 
    singleSelect: null, 
    multipleSelect: [], 
    option1: 'option-1', 
    }; 

    $scope.forceUnknownOption = function() { 
     $scope.data.singleSelect = 'nonsense'; 
    }; 
}]); 
})(window.angular); 

現在,我想在下拉菜單中顯示此選項數組。該腳本如下:

<body ng-app="staticSelect"> 
    <div ng-controller="ExampleController as ctrl"> 
    <form name="myForm"> 
     <label for="singleSelect"> Single select: </label><br> 
     <select name="singleSelect" ng-model="data.singleSelect" 
     ng-options="ctrl.option.id as ctrl.option.label for ctrl.option in ctrl.options"> 
     </select><br> 
     <tt>selected = {{data.singleSelect}}</tt><br/> 
    </form> 
    </div> 
</body> 

如果選擇數組是$scope那麼這個工作正常。我是AngularJS的新手。在使用控制器變量時,我在這裏做錯了什麼?

回答

0

這是你的NG-選項如何屬性應該是這樣的:

ng-options="option.id as option.label for option in ctrl.options" 
+0

感謝you..it解決我的問題 – AdtG