Joel Wilson
Joel Wilson

Reputation: 677

ng-model set value of select

This is my angularjs directive:

dicApp.directive('listpanel', function ($resource) {
    return {
        restrict: 'E',
        template: '<select ng-model="selectedDictionary" ng-change="listChange()">' +
            '<option value="">--Select--</option>' +
            '<option ng-repeat="item in items" value="{{item.value}}">{{item.label}}</option> ' +
            '</select>',
        replace: true,
        link: function (scope, elem, attrs) {
            var list = $resource('http://test.com')
            list.get(function (data) {
                scope.items = [];

                for(var index = 0; data.documents[0].Dictionaries.length > index; index++){
                    scope.items.push({'label': data.documents[0].Dictionaries[index].Label.text, 'value': data.documents[0].Dictionaries[index].Glossaries._id});
                }

                scope.selectedDictionary = '51639519ed7f3dd05869c3d9';

                console.log(data.documents[0].Dictionaries[0].Glossaries._id);
                //scope.selectedDictionary = data.documents[0].Dictionaries[0].Glossaries._id;

            });
        }
    }
});

I am pretty sure the value 51639519ed7f3dd05869c3d9 exists in scope.items that is bound, yet the selectedDictionary is never selected in the select. What am I missing?

Upvotes: 0

Views: 310

Answers (1)

Hossain Muctadir
Hossain Muctadir

Reputation: 3626

Try creating options this way <select ng-model='data' ng-options="item for item in list"> </select>

Upvotes: 1

Related Questions