2015-11-05 72 views
1

當使用Angular的$ q時,爲什麼一切都在$$狀態對象下面?我曾經有過這樣的印象:雙美元在Angular中意味着私密性,在這種情況下這對我來說並不合理。我是否錯誤地實現了我的承諾?

只是測試,此方法返回$$狀態對象:

$scope.makePromise = function(){ 
    return $q(function(resolve, reject){ 
    resolve('Promise Resolved'); 
    }); 
} 
+0

你保證是正確的。正如文檔所說:它是ES6承諾的構造器。 – Errorpro

+0

你還期望它會返回什麼? – Bergi

回答

0

嘛,$$state是如何角跟蹤其承諾的內部狀態。你不應該在你自己的代碼中使用它,它可能會在未來破裂。

相反,如果你想查詢的承諾的狀態 - 你應該添加一個then處理它:

myPromise.then(function(el){ 
    // I got here after the promise resolved and `el` is the value 
    // it fulfilled with. 
}, function(e){ 
    // I got here after the promise rejected and `e` is the error 
    // it rejected with. 
});