Search code examples
javascriptangularjsng-options

Unable to set initial value when using ng-options




I have looked over the 'questions that may have your answer' without any luck. nothing worked for me there :(

Although it seems that I have a very simple case, something goes wrong...

I have the following HTML:

<body>
  <div ng-controller="eventController as eventCtrl">
    <select ng-init="eventCtrl.userDetails.station = eventCtrl.eventDetails.stations[0]" ng-model="eventCtrl.userDetails.station" ng-options="o as o for o in eventCtrl.eventDetails.stations"></select>
  </div>
</body>

And my angular app looks like this:

(function() {
var app = angular.module('eventRegistration', []);
var conn = new dataConn();

app.controller('eventController', ['dataService', function(dataService) {
    var eventCtrl = this;
    eventCtrl.eventDetails = {};
    eventCtrl.userDetails = {};


    dataService.getActiveEvents()
        .then(function (events) {
            eventCtrl.eventDetails = events[0];
        });        
}]);

app.service('dataService',['$q', function($q) {
    return {
        getActiveEvents: function() {
            var deferred = $q.defer();
            conn.getActiveEvents(function(events) {
                deferred.resolve(events)
            });
            return deferred.promise;
        }
    }
}]);
})();

My eventCtrl.eventDetails object will finally looks like this (I'm getting an array of those from the server and for now using only the first one):

    {
        "date":new Date(2014, 10, 2),
        "isActive": true,            
        "stations":['STATION_A', 'STATION_B'],                        
        "comments":""
    }

I'm trying to set the model of the user's selected station to eventCtrl.userDetails.station and initiate its value to the first station of eventCtrl.eventDetails.stations using ng-init.

It is important to mention that the stations data (all event details actually) is being fetched async (using deferred object).

The result of my code is a selection box with 'STATION_1' and 'STATION_B' values, but I have an additional first empty value. it seems that something is wrong in my the ng-init definition.
Any ideas?


Thanks so much!


Solution

  • **ng-selected** will fit your need.
    After making an async call to the server, assign the value which you want to initialize to a scope variable and use that variable in ng-selected.
    
    `<select ng-if="selecteditem" ng-model="selecteditem" ng-options="item for item in items.name" ng-selected="selecteditem">
      </select>`
    
    Here selecteditem is the variable which you want to initialize.
    
    `myapp.controller('ItemCtrl',['$scope','$http', function($scope,$http)  //No I18N 
    {  
      $scope.items = {"name":['first','second']}
      $scope.selecteditem = $scope.items.name[0]
    }]);`
    
    
    Since you mentioned an async call use **ng-if** such that select will be displayed only it has data.You can display some default values using **ng-if="!selecteditem"**
    
    see the plnkr http://plnkr.co/edit/hv2OI1If1FMGhV0dgj9a?p=preview