3 回答

TA貢獻(xiàn)1921條經(jīng)驗(yàn) 獲得超9個(gè)贊
但是,如果我想在回調(diào)后使用$ scope.tempObject,那么該如何使用它。?
您需要鏈從httpPromise。保存httpPromise并將值返回到onFullfilled處理函數(shù)。
//save httpPromise for chaining
var httpPromise = $http({
method: 'GET',
url: '/myRestUrl'
}).then(function onFulfilledHandler(response) {
$scope.tempObject = response
console.log("Temp Object in successCallback ", $scope.tempObject);
//return object for chaining
return $scope.tempObject;
});
然后你外面連鎖從httpPromise。
httpPromise.then (function (tempObject) {
console.log("Temp Object outside $http ", tempObject);
});
有關(guān)更多信息,請(qǐng)參見《AngularJS $ q服務(wù)API參考-鏈接承諾》。
可以創(chuàng)建任何長(zhǎng)度的鏈,并且由于一個(gè)承諾可以用另一個(gè)承諾來解決(這將進(jìn)一步推遲其解決方案),因此可以在鏈中的任何點(diǎn)暫停/推遲對(duì)承諾的解決。這樣就可以實(shí)現(xiàn)功能強(qiáng)大的API。1
基于承諾的異步操作的解釋
console.log("Part1");
console.log("Part2");
var promise = $http.get(url);
promise.then(function successHandler(response){
console.log("Part3");
});
console.log("Part4");
PIC
“ Part4”的控制臺(tái)日志不必等待數(shù)據(jù)從服務(wù)器返回。XHR 啟動(dòng)后立即執(zhí)行。“ Part3”的控制臺(tái)日志在成功處理程序函數(shù)內(nèi)部,該函數(shù)由$ q服務(wù)保留,并在從服務(wù)器到達(dá)數(shù)據(jù)并且XHR 完成后調(diào)用。
但是,如果我想在回調(diào)后使用$ scope.tempObject,那么該如何使用它。?
您需要鏈從httpPromise。保存httpPromise并將值返回到onFullfilled處理函數(shù)。
//save httpPromise for chaining
var httpPromise = $http({
method: 'GET',
url: '/myRestUrl'
}).then(function onFulfilledHandler(response) {
$scope.tempObject = response
console.log("Temp Object in successCallback ", $scope.tempObject);
//return object for chaining
return $scope.tempObject;
});
然后你外面連鎖從httpPromise。
httpPromise.then (function (tempObject) {
console.log("Temp Object outside $http ", tempObject);
});
有關(guān)更多信息,請(qǐng)參見《AngularJS $ q服務(wù)API參考-鏈接承諾》。
可以創(chuàng)建任何長(zhǎng)度的鏈,并且由于一個(gè)承諾可以用另一個(gè)承諾來解決(這將進(jìn)一步推遲其解決方案),因此可以在鏈中的任何點(diǎn)暫停/推遲對(duì)承諾的解決。這樣就可以實(shí)現(xiàn)功能強(qiáng)大的API。1
基于承諾的異步操作的解釋
console.log("Part1");
console.log("Part2");
var promise = $http.get(url);
promise.then(function successHandler(response){
console.log("Part3");
});
console.log("Part4");
PIC
“ Part4”的控制臺(tái)日志不必等待數(shù)據(jù)從服務(wù)器返回。XHR 啟動(dòng)后立即執(zhí)行?!?Part3”的控制臺(tái)日志在成功處理程序函數(shù)內(nèi)部,該函數(shù)由$ q服務(wù)保留,并在從服務(wù)器到達(dá)數(shù)據(jù)并且XHR 完成后調(diào)用。
console.log("Part 1");
console.log("Part 2");
var promise = new Promise(r=>r());
promise.then(function() {
console.log("Part 3");
});
console.log("Part *4*");

TA貢獻(xiàn)1906條經(jīng)驗(yàn) 獲得超10個(gè)贊
$ http調(diào)用是異步調(diào)用?;卣{(diào)函數(shù)在返回響應(yīng)后執(zhí)行。同時(shí),該函數(shù)的其余部分繼續(xù)執(zhí)行,并將$ scope.tempObject記錄為{}。解析$ http時(shí),僅設(shè)置$ scope.tempObject。Angular將使用兩種方式自動(dòng)綁定更改的值。
視圖中的{{tempObject}}會(huì)自動(dòng)更新。
如果要在回調(diào)后使用tempObject,請(qǐng)執(zhí)行此操作
then(function(data){
onSuccess(data);
},function(){
});
function onSuccess(data){
// do something
}

TA貢獻(xiàn)1842條經(jīng)驗(yàn) 獲得超13個(gè)贊
這意味著如果我們$timeout
延遲使用函數(shù),那么它也會(huì)獲取完整的數(shù)據(jù)。例如$timeout(function () { console.log("Temp Object outside $http ", $scope.tempObject); }, 1000)
- 3 回答
- 0 關(guān)注
- 733 瀏覽
添加回答
舉報(bào)