ng-options dont work with an object

Im sure there's a perfectly good explanation for this, but i cant seem to make ng-options work with my model object. Basically i would like to make my select show a list of countries. Option values are country codes and labels are country names:

HTML

<div ng-app="demoApp">
    <div ng-controller="UserInfoCtrl">
        <h1>User Info</h1>

        <label>Email</label>
        <input type="text" ng-model="user.email" />
        <br />

        <label>Country</label>
        <select ng-model="user.country" ng-options="code as name (code, name) in countriesByCode">
        </select>

        <pre>{{ user | json }}</pre>
    </div>
</div>

JavaScript

var demoApp = angular.module('demoApp', []);

demoApp.controller("UserInfoCtrl", function($scope) {
    $scope.user = { };

    $scope.countriesByCode = {
        'AF' : 'Afghanistan',
        'CA' : 'Canada',
        'RU' : 'Russia'
    };
});

Here's the jsfiddle

Answers


You are missing a 'for' in your statement

code as name for (code, name) in countriesByCode

Here is the corrected fiddle: http://jsfiddle.net/8XJX4/2/

hope this helps.


Sorry, that was a silly mistake in my expression. Instead of

code as name (code, name) in countriesByCode

it should be

code as name for (code, name) in countriesByCode


Need Your Help

Where is the source and destination address fields in TCP header?

tcp ip ip-address packet

From what I've read, TCP sits on the layer between the application and IP, and handles setting up the packets, checking for errors, ordering etc so the application itself doesn't have to do it.

Convert XLS to XLSB Programatically?

c# vb.net sharepoint binary xls

I have a customer that needs to convert XLS files to XLSB. Has anyone done this programatically, (with or without an add-on --- doesn't matter --- just need to be able to automate it)? I'm looking ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.