Ng-options with object data source

I am trying to understand how ng-options work with a data source. I read the documents and I feel that I am doing exactly what is required, but I still have problems trying.

<!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> 

I always get this error in the console:

Mistake:

Expected ngOptions in the form of 'select (as label)? for (key,)? value in collection (track by expr)? 'but got' k as v for (k, v) in types.

What am I doing wrong?

See plunkr here:

http://plnkr.co/edit/Bl6u4151KyDkxhYrsdCm?p=preview

+49
angularjs ng-options
Dec 05 '13 at 6:04 on
source share
1 answer

This is strange, but it seems that you need to put a space after the for . It works:

 <select ng-model="type" ng-options="k as v for (k, v) in types"> <option value="">Select Type</option> </select> 
+92
Dec 05 '13 at 6:15
source share



All Articles