user500468
user500468

Reputation: 1221

ng-options with one object

I have a callback that either can be an array with multiple objects, or just one object:

   $http.get($rootScope.appUrl + '/nao/abb/getStationData/' + selectedSupercustomer.superkund_id).success(function(data) {
        $scope.stationOptions = data;
        if($scope.stationOptions instanceof Array)
        {
            //Array of objects
            angular.forEach($scope.stationOptions, function(option) {
                console.log(option);
                if(option.nao_adsl_stationer_id === $scope.abbData.nao_adsl_stationer_id) {
                    $scope.selectedStation = option;
                }
            })
        }
        else
        {
            //Only one object
            if($scope.stationOptions.nao_adsl_stationer_id === $scope.abbData.nao_adsl_stationer_id) {
                $scope.selectedStation = $scope.stationOptions;
            }
        }
    });

If It Is an array with multiple objects, I don't have any problem to loop them out with ng-options, but when the callback is one object, like this:

{"nao_adsl_stationer_id":"33","namn":"Malm\u00f6 - Arl\u00f6v","stationskod":"ARV","ipserie_id":"","net":"","mask":"","ip":""}

It don't work.

I use ng-options like this:

 <td><select class="form-control input-sm2" ng-disabled="!selectedSupercustomer" ng-model="selectedStation" ng-options="item as item.namn for item in stationOptions" ng-change="onChangeStation(selectedStation)"><option value=''>Välj station</option></select></td>

Upvotes: 0

Views: 52

Answers (1)

devnull69
devnull69

Reputation: 16544

Either you return an array with only one object in it (so you could get rid of the else part of your code) or you construct this array inside the else part like this

$scope.selectedOptions = [data];
$scope.selectedStation = $scope.stationOptions[0];

Upvotes: 1

Related Questions