我很难理解ng选项如何工作与数据源。我读过文档,我觉得我做的正是所需要的,但我仍然遇到问题时尝试。
<!DOCTYPE html> <html ng-app="app"> <head> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js"> </script> <link rel="stylesheet" href="style.css" /> <script type="text/javascript"> angular.module('app',[]) .controller('IndexCtrl',['$scope',function($scope) { $scope.types = { 1: "value1",2: "value2",5: "value3" }; }]); </script> </head> <body ng-controller="IndexCtrl"> <select ng-model="type" ng-options="k as v for(k,v) in types"> <option value="">Select Type</option> </select> </body> </html>
我总是在控制台中得到这个错误:
错误:
Expected ngOptions in form of ‘select (as label)? for (key,)?value in collection (track by expr)?’ but got ‘k as v for(k,v) in types’.
我做错了什么?
请参见plunkr:
这是一种奇怪,但它似乎你需要放一个空间后。这工作:
<select ng-model="type" ng-options="k as v for (k,v) in types"> <option value="">Select Type</option> </select>