'铬'在AngularJS $窗口服务中未定义对象

时间:2014-03-24 15:40:39

标签: angularjs google-chrome-app

我正在使用AngularJS开发Chrome应用程序。我的应用程序使用chrome.storage,因此我创建了一个包装器以便使用它。包装器代码如下所示:

angular.module('myApp').factory('chromeStorageApi', function ($window, $q) {
  var deferred = $q.defer();

  return {
    set: function (obj) {
      if ('chrome' in $window && typeof  $window.chrome.storage === 'undefined') {
        return deferred.reject();
      } else {
        $window.chrome.storage.local.set(obj, function(){
          if($window.chrome.runtime.lastError !== 'undefined') {
            return deferred.reject();
          }
          deferred.resolve();
        });
      }
     ................

我有另一项服务(' storageService'),它将我的包装器作为依赖项:

angular.module('myApp').factory('storageService', function ($q, $rootScope, $http, chromeStorageApi) {
.......

问题是storageService中的 $ window 有' chrome'对象和它可以在那里使用chrome.storage,但在我的包装&#chrome;'对象获取' undefined'。

TypeError: 'undefined' is not an object (evaluating '$window.chrome.storage')

我想了解为什么会这样。在一个地方 $ window 有' chrome'对象,但在另一个没有。

感谢。

0 个答案:

没有答案