colefner colefner - 1 year ago 119
AngularJS Question

ng-options with object data source

I'm struggling to understand how ng-options works with a data source. I've read the docs and I feel like I'm doing exactly what is required, but I still get problems when attempting.

<!DOCTYPE html>
<html ng-app="app">
<script src=""> </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"
<body ng-controller="IndexCtrl">
<select ng-model="type" ng-options="k as v for(k, v) in types">
<option value="">Select Type</option>

I always get this error in the console:


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'.

What am I doing incorrectly?

See plunkr here:

Answer Source

This is kind of strange, but it seems like you need to put a space after for. This works:

<select ng-model="type" ng-options="k as v for (k, v) in types">
  <option value="">Select Type</option>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download