AngularJS:使用异步chrome.storage.local.get()的奇怪的console.log行为

时间:2014-01-03 18:23:57

标签: javascript angularjs google-chrome-extension promise console.log

如果您需要更多信息来解决此问题,请与我们联系。

我正在尝试使用工厂检索本地数据并从控制器调用它。

Controller.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的奇怪行为吗?我也是承诺的新手,所以这可能是我搞砸了,虽然在工厂中使用回调而不是承诺时,这也表现相同。

0 个答案:

没有答案
相关问题