Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS - Get data immediatelly after call to query()

I have a Service

angular.module('AigServices', ['ngResource']).factory('aiguilleurs', function($resource){

var service = $resource('app/data/EpargneATermeRachetableCELI.json', {}, {
    query: {method:'GET', params:{}, isArray:true}});

return service;  });

In my controller, when I call aiguilleurs.query(), I receive the structure but no data like it said in the doc. I want to have the data has soon has I call the query(). Any idea? I have looked around and didn't find anything. So the controller code follow ($scope.listeAiguilleurs[0] is always undefined):

$scope.listeAiguilleurs = aiguilleurs.query();   
var questionEnCours = null;

if ($scope.listeAiguilleurs[0] != null) {
    questionEnCours = $scope.listeAiguilleurs[0].questions[$scope.noQuestionEnCours-1];
}
$scope.questionEnCours = questionEnCours;
like image 566
user1756481 Avatar asked Dec 06 '25 02:12

user1756481


1 Answers

That is because the query returns a promise. You can read more about that here. You have to pass a callback to the function (check out the Credit Card Resource example here). Try this:

$scope.listeAiguilleurs = aiguilleurs.query(function () {   
    var questionEnCours = null;

    if ($scope.listeAiguilleurs[0] != null) {
        questionEnCours = $scope.listeAiguilleurs[0].questions[$scope.noQuestionEnCours-1];
    }
    $scope.questionEnCours = questionEnCours;
});
like image 62
Max Avatar answered Dec 08 '25 14:12

Max