AngularJS $http 响应

AngularJS $http response

这是我的代码

var object = null
app.controller("controller",function($scope,service){
    $scope.search=function(){
        service.findData().then(
            function successCallback(response){
                object = response.data
            },
            function errorCallback(response){
                alert("error")
            }
        )
        console.log(object)
    }
})

当我打印这个对象时,它是空的,所以问题是:我怎样才能 response.data 到外面?

感谢您的帮助!

在AngularJS时代,将控制器分配给变量是一种普遍做法。

app.controller("controller",function($scope,service){
    var self = this;
    $scope.search=function(){
        service.findData().then(
            function successCallback(response){
                self.yourBusinessFunction (response.data)

            },
            function errorCallback(response){
                alert("error")
            }
        )
        console.log(object)
    }

    self.yourBusinessFunction = function(data){

    console.log(data);
    // data here will be the response.data from the service
    // You can write your logic here

    }


})