1
var myApp = angular.module('myRealApp',[]);

   myApp.controller('myController',function($scope,$http){
   "use-strict";

   $scope.content = [];

   $scope.fetchContent = function () {
    $http({
         method : 'POST',
         ContentType : "application/json",
         url : "http://localhost:8080/myUrl",
         headers : { 'Content-Type' : 'application/json'}   
    })
    .success(function(response){
         $scope.content = response;
         console.log($scope.content); //Shows the returned object from server

    });
   }

   $scope.fetchContent();
   console.log($scope.content); //Shows as []
}                   

When I load page, $scope.fetchContent gets called and gets the data from the server, also assigns it to $scope.content. But, when I access $scope.content outside this function, it still shows it's value as []. What wrong I am doing here ?

Data returned from server is a list of objects. (ArrayList of Java to JSON array).

1
  • 1
    it is happening because of async behavior of ajax request. You should read some articles of it. Commented Jul 26, 2015 at 16:24

2 Answers 2

3

$http is asynchronous. The order of your process is like this:

-> fetchContent()
-> console.log($scope.content) // outside of your $http
-> $http.success

The content will be shown in your html template anyway because angular is watching your scope, so dont be worry about that. If you want to run another process after fetching data, call the function in your success callback function

Sign up to request clarification or add additional context in comments.

2 Comments

Is this the usual practice to run another process in success callback function? I was trying to the concerns.
better to use the promise as @deKajoo mentioned in his answer. In that case the code is easy to read and easy to maintain.
3

$http create a promise that will be resolved when the ajax call to http://localhost:8080/myUrl is resolved. When the browser arrive at the line console.log($scope.content); (few lines after the promise has been created) the promise isn't resolved yet.

If you want to do stuff (like a console.log) when your promise is resolved outside of the .success() you have to get the promise and use a .then() on the promise $http has created like this :

"use-strict";
var myApp = angular.module('myRealApp',[]);

myApp.controller('myController',function($scope,$http){

    $scope.content = [];

    $scope.fetchContent = function () {
       return $http({ //line changed
           method : 'POST',
           ContentType : "application/json",
           url : "http://localhost:8080/myUrl",
           headers : { 'Content-Type' : 'application/json'}   
       });
   }

    var promise = $scope.fetchContent();
    promise.then(function() { //line changed
        console.log($scope.content);
    });
}
  • side note: You should put the "use-strict"; line on top of your file
  • side-note²: Beware that creating your controller like this : myApp.controller('myController', function($scope,$http) { ... }) will create error when minifying prefer the redondant version : myApp.controller('myController', ["$scope", "$http", function($scope,$http) { ... }])

2 Comments

Thanks. I understand it now. However, this exact code is showing me error: "Cannot read property 'then' of undefined" at line where "promise.then .." is being called. I searched for this but the syntax seems fine.
My bad you don't need the .promise after the $http I mixed it up with $q :) (post updated) docs.angularjs.org/api/ng/service/$http

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.