1

I have my angularjs view in wich I must populate my select box with value saved in database, and to allow for other options to be selected. I have tryed like this:

<div class="form-group">
 <label class="control-label" for="status">Status zaposlenika</label>
 <div class="controls">
 <select required name="status" class="form-control" ng-model="employee.status"  ng-options="statusType.name for statusType in statusTypes" >
</select>
</div>

But my value is not populated in my view. ( {{employee.status}} - > "test" )

$scope.statusTypes = [

     {
        name: 'test'
     },

    {
        name:'Test1'
    },

    {

        name: 'Test2'
    },

    {

        name:'Test3'
    }
];

How can I do this ?

EDIT

My model employee.status is populated with value "test". But my select box is not. Othe values are listed as items for selection. How can I set default value that is saved in my database to be pre-selected in my select box.

Sysrq147
  • 1,359
  • 4
  • 27
  • 49

1 Answers1

1

Your model employee.name is a string and selectbox is bound to an object similar to {name: "Test1"}. So if you want to select option from statusTypes you have to find corresponding object in array of object.

$scope.statusTypes = [
    {name: 'Test1'},
    {name: 'Test2'}, 
    {name: 'Test3'}
];

var selectedStatus = $scope.statusTypes.filter(function(type) {
    return type.name = 'Test2';
})[0];

$scope.employee = {
    status: selectedStatus
};

So you have to make employee.status to be one of the objects from statusTypes array.

Or other option is to continue to use string for employee.status and change ngOptions to bind to a string instead of object:

ng-options="statusType.name as statusType.name for statusType in statusTypes"
dfsq
  • 191,768
  • 25
  • 236
  • 258
  • One very strange thing happen's, when i upadate my values, sometimes it works sometime it wont work :( Sometime value is bound to that string sometimes it is not. I am using secound option. – Sysrq147 Nov 03 '14 at 13:39
  • I'm afraid "sometimes it wont work" is really hard to debug.. Maybe you can replicate the issue somehow in plunkr? Otherwise it's hard to find a problem. – dfsq Nov 03 '14 at 13:40
  • Thank you for your help, it is hard to replicate problem. I get my data from MongoDB with Node.js as back-end. Using AnlugarJS factory and $resource. Strange thing is that my employee model is populated in wright way (all data is there when I "print" {{employee}} in my view.) But sometimes my select box is not showing data that is in employee.status ..Wierd – Sysrq147 Nov 03 '14 at 13:48
  • I get this strange extra option when it is not working : . Related question is [link]http://stackoverflow.com/questions/16783294/angular-adds-strange-options-into-select-element-when-setting-model-value[/link] – Sysrq147 Nov 03 '14 at 17:23
  • It happens when you use ng-options="statusType.name as statusType.name.. ? – dfsq Nov 03 '14 at 17:42
  • Yes, I have solved it by "manualy" adding options. e.g. . Now it works fine. I am sorry for my bad language skills. I am intrested why is that happening. – Sysrq147 Nov 03 '14 at 17:46
  • I'm also very interested why this ?strange ? option appears. Maybe I will try to find the answer in source code. – dfsq Nov 03 '14 at 17:50