$ resource problems Angularjs

1

I'm having a problem using $ resource from angularjs. The data coming from the API is only displayed in the service but when I call in the controler the list comes empty.

//service
angular.module('core.rota').
    factory('Rota', function ($resource,$cookies, $location, $httpParamSerializer,LoginRequiredInterceptor) {
        var url =  '/api/rota/:id/'

                //Carregamento dos locais
        var locaisQuery = {
            url:'/api/core/local/',
            method:"GET",
            params: {},
            isArray: true,
            cache:false,
            interceptor: {responseError: LoginRequiredInterceptor},
            transformResponse: function (data, headersGetter, status) {
               var finalData = angular.fromJson(data)
                console.log(finalData)// ok os dados são exibidos
               return finalData.results
            }
        }

        var token = $cookies.get("token")
        if (token){
            minhasRotasQuery["headers"] = {"Authorization": "JWT " + token}
            rotasQuery["headers"] = {"Authorization": "JWT " + token}
            locaisQuery["headers"] = {"Authorization": "JWT " + token}     
        }

        return $resource(url, {},{
            minhas:minhasRotasQuery,
            todas:rotasQuery,
            query:locaisQuery,
            get:getRota,
            create:createRota,
            createServ:createServico,
            // delete: commentDelete,
            update: updateRota,
        })
});

//component
angular.module('rotacreate').
        component('rotacreate',{
            templateUrl:'/api/templates/rotas/rotacreate.html',
            controller: function(Rota,User,$cookies, $http, $location, $routeParams, $scope,$filter,moment,LoginRequiredInterceptor){

            Rota.query(function(data){
                    console.log(data)//Array vazio Array[ ]
                    $scope.users = data
                },function (err_data) {
                    console.log(err_data)
                })
}
    
asked by anonymous 30.01.2017 / 19:15

1 answer

0

Your current code is not executing the method and waiting for the promise resolution - you are actually just passing objects to the method.

Change from

Rota.query(function(data){ [...]

for

Rota.query().then(function(data){ [...]

or

Rota.query().$promise.then(function(data){ [...]

Depending on your version.

    
02.02.2017 / 15:27