如何分配数据从函数返回到$ scope变量
问题描述:
这里是我的代码,
$scope.output=abc(1,2);
function abc(mid, type) {
$http({
...
}
}).then(function (response) {
return response.data;
}, function (response) {
});
}
console.log($scope.output)
$ scope.output
是
不确定
的功能是执行,但数据没有分配到$ scope变量
答
这是因为abc(1,2)
是一个异步调用,所以
试试这个
var res =abc(1,2);
function abc(mid, type) {
$http({
...
}
}).then(function (response) {
$scope.output = response.data;
console.log($scope.output)
return response.data;
}, function (response) {
});
}
答
abc(1,2);
function abc(mid, type) {
$http({
...
}
}).then(function (response) {
$scope.output = response.data;
}, function (response) {
});
}
console.log($scope.output)
在异步操作,则无法使用返回
答
而不是在async call
中使用return
。您可以直接将response.data分配到$scope.output
变量中。
abc(1,2);
function abc(mid, type) {
$http({
...
}
}).then(function (response) {
$scope.output = response.data;
}, function(error) {
});
}
一旦它的返回执行指针退出功能,所以console.log()不会执行 –
我已经编辑它,现在你可以尝试 –