I have this view code:

> <label class="item item-input item-select">    <i class="icon
> ion-android-map icon-color"></i>&nbsp;
>       <span class="input-label">Estado</span>
>       <select ng-model="meuEstado" required>
>         <option value="0">Selecione...</option>
>         <option ng-repeat="estado in listaEstados" value="{{ estado.idEstado }}">{{ estado.nmEstado }}&nbsp;-&nbsp;{{
> estado.ufEstado }}</option>
>       </select>    </label>

Below is my service:

var starter = angular.module('starter.services', []);

starter.factory('appFactory', ['$http', function($http) {

        listaEstados: function(local) {

            var resultado = null;

            var dataToSend = {
                type   : 'recuperarestados'
            };

            $http({
                url: 'http://....',
                method: 'POST',
                data: dataToSend
            }).success(function(response, status, headers, config) {
                return resultado = response;
            }).error(function(err, status, headers, config) {
                return resultado = null;      
            }); 
        }
    };
}]);

This is the json returned:

[{ "idEstado" : "7", "ufEstado" : "DF", "nmEstado" : "Distrito Federal"},{ "idEstado" : "9", "ufEstado" : "GO", "nmEstado" : "Goiás"},{ "idEstado" : "11", "ufEstado" : "MG", "nmEstado" : "Minas Gerais"},{ "idEstado" : "19", "ufEstado" : "RJ", "nmEstado" : "Rio de Janeiro"},{ "idEstado" : "23", "ufEstado" : "RS", "nmEstado" : "Rio Grande do Sul"},{ "idEstado" : "26", "ufEstado" : "SP", "nmEstado" : "São Paulo"}]

And here is part of my controller:

starter.controller('homeCtrl', ['$scope', '$location', '$state', '$ionicHistory', '$ionicLoading', '$timeout', 'appFactory', 
    function($scope, $location, $state, $ionicHistory, $ionicLoading, $timeout, appFactory) {

    $scope.meuEstado = "0"
    $scope.listaEstados = [];

    $ionicLoading.show({
        templateUrl:"templates/spinner.html",
        animation: 'fade-in',
        showBackdrop: true,
        maxWidth: 200,
        showDelay: 0
    });

    $scope.listaEstados = appFactory.listaEstados(local);

    //Define o tempo para remover o loader, 
    $timeout(function () {
        $ionicLoading.hide();
    }, 2000);
}]);

My problem is that my service can get the data from my back-end, but I can´t get this data from my controller, to load my $scope.listaEstados on the view also.

I notice in some posts here in Stackoverflow that I need to use "$scope.$apply", but I have already tried that without success.

Can someone please tell me what kind of change I need to do in my code, to get the data in the controller ?

Thanks.

share|improve this question

You should do it with $q

Service:

starter.factory('appFactory', ['$http','$q', function($http,$q) {


    listaEstados: function() {
        var deferred=$q.defer();
        var resultado = null;

        var dataToSend = {
            type   : 'recuperarestados'
        };

        $http({
            url: 'http://....',
            method: 'POST',
            data: dataToSend
        }).success(function(response, status, headers, config) {
            deferred.resolve(response);
        }).error(function(err, status, headers, config) {
            deferred.reject(response);    
        }); 
        return deferred.promise;
    }
};
}]);

In Controller:

starter.controller('homeCtrl', ['$scope', '$location', '$state', '$ionicHistory', '$ionicLoading', '$timeout', 'appFactory', 
function($scope, $location, $state, $ionicHistory, $ionicLoading, $timeout, appFactory) {

$scope.meuEstado = "0"
$scope.listaEstados = [];

$ionicLoading.show({
    templateUrl:"templates/spinner.html",
    animation: 'fade-in',
    showBackdrop: true,
    maxWidth: 200,
    showDelay: 0
});

appFactory.listaEstados().then(function(success){
     $scope.listaEstados=success;
},function(error){
     //Something to handle error
})

//Define o tempo para remover o loader, 
$timeout(function () {
    $ionicLoading.hide();
}, 2000);

}]);

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.