这是我的代码
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?
感谢您的帮助!
最佳答案
将 Controller 分配给变量是 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
}
})
关于AngularJS $http 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53431922/