AngularJS-$ scope异地$ http

I've create this piece of code:

app.controller('SiteDetailCtrl',function($scope, $routeParams, $http){
    //remove ":" in SiteId
    var SiteId = $routeParams.SiteId.replace(':','');

    $scope.Site = $http.get('path-to-ajax/Site/'+SiteId);
        .success(function(data){
            $scope.Site = data;
            console.log($scope.Site);
        })
        .error(function(){
            $scope.Site = 'NULL';
            alert('Ajax Fail');
        });
        console.log($scope.Site);
});

I don't understand why $scope.Site is available in Success function but outside ($http) $scope.Site is null. Please explain for me what happen here.

I'm new to AngularJS.

查看全部
weixin_33739523
weixin_33739523
2014/03/23 12:26
  • http
  • angularjs
  • ajax
  • 点赞
  • 收藏
  • 回答
    私信

3个回复