json - AngularJs : Multiple Calls for $http service -


this code :

        var updatescreen = function() {           $http.get("http://localhost:5000").then(function(response){           $scope.numserie = response.data.refdetail.num_serie;            $http.get("data/tempsgammes.json").then(function(res){             $scope.time = res.data[$scope.numserie].temps;             console.log($scope.time)           })           console.log($scope.time)        } 

the first $http.get("http://localhost:5000") returns list of details use in rest of code, interests variable numserie.

depending on value of numserie, $http.get("data/tempsgammes.json") executed extract time numserie json file.

the problem cannot access $scope.time in second call $http. first console.log() gave me desired result, second 1 undefined.

can me ?

edit: console.log($scope.time) being called 2 times. see first 1 giving right value , second 1 isn't. because of async behaviour of "then". not intuitive second console.log($scope.time) being called before "then" has finished, why shows "undefined".

edit 2: try following probe execution order on async operations

 $http.get("data/tempsgammes.json").then(function(response){    alert('first');       // <--- called first    .then(function(res){         alert('third!');   // <--- called third  });  alert('second!');       // <--- called second 

yes, can use $scope.time anywhere, if try use bellow of "then", not defined yet. can put somewhere in view {{$scope.time}} , probe defined outside of $http.get


if undefined probable index of res.data[$scope.numserie] out of bounds.

first check if have error using developer tools of browser. $scope.numserie seems culprit here, check value or provide more detail


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -