如果您需要更多信息来解决此问题,请告诉我。
我正在尝试使用工厂检索本地数据并从控制器调用它。
控制器.js:
storage.getAllLocalInfo().then(function(data){
console.log(data.distractions[0].type);
// produces 'url'
// console.log(data.distractions);
// produces the following
// 0: Object
// oldTxt: "youtube.com"
// txt: "youtube.com"
// type: undefined
$scope.distractions = data.distractions;
// This only happens when executing the line above.
// Without that line, there is no inconsistency.
});
如果我要求嵌套属性(type
),控制台返回正确的值,但是当我要求整个对象时,type
如何返回为undefined
. 这只发生在我包含$scope.distractions
. 以及相关的工厂:
var getAllLocalInfo = function() {
var deferred = $q.defer();
chromeStorage.get( null , function( data ) {
if (!data) {
deferred.reject();
} else {
deferred.resolve(data);
}
});
return deferred.promise;
};
谁能解释控制器中 console.log 的奇怪行为?我对 Promise 也很陌生,所以这可能是我搞砸的,尽管在工厂中使用回调而不是 Promise 时,这也表现相同。