fusio
fusio

Reputation: 3675

Cannot set option value in select programmatically using AngularJs

The select:

select(ng-model="elId", ng-change="load(elId)", ng-options="e._id as e.name for e in options")
    option(value="") - select a mashup -

Controller:

The following works, the select gets populated. I am using broadcasts because it comes from a socket.. don't mind that.

 //receive list
 $scope.$on('list:refresh', function(ev, data) {
     $scope.options = data
 })

The following only works if I did not manually select any option before. data.id is a valid entry in the list.

 //refresh list of mashups when a new one has been created
 $scope.$on('list:select', function (ev, data) {  
     $scope.elId = data.id
 })

If I manually select an option, and then the list:select fires, the $scope.elId is udated but the <select> does not highlight the option.

What is going on?

Upvotes: 0

Views: 3628

Answers (1)

fusio
fusio

Reputation: 3675

Solved:

The select:

select(ng-model="data.elId", ng-change="load()", ng-options="e._id as e.name for e in options")
    option(value="") - select a mashup -

Controller:

 //receive list
 $scope.$on('list:refresh', function(ev, data) {
     $scope.options = data
 })

 $scope.load() {
    var elId = $scope.data.elId
    ....
 }

 //refresh list of mashups when a new one has been created
 $scope.$on('list:select', function (ev, data) {  
     $scope.data.elId = data.id
 })

I guess the problem was that I tried to change the selected value from a child controller.. now I am using a service Data that I load into the controllers $scope.data = Data.. meh?

Upvotes: 1

Related Questions