2

我正在尝试执行以下操作:

var myApp = angular.module('myApp', []);
myApp.provider('routeResolver', ['$q','$rootScope',function($q, $rootScope)
    {        
        console.log( $q, $rootScope );
        this.$get = function()
        {
            return this;
        }
    }]

);

但是,这给出了错误:Unknown Provider $q

所以我将代码更改为以下内容:

myApp.provider('routeResolver', ['$qProvider','$rootScopeProvider',function($q, $rootScope)
    {        
        console.log( $q.defer() );
        console.log( $rootScope );
        this.$get = function()
        {
            return this;
        }
    }]
);

然而,这给出了错误uknown function.。甚至在做:

console.log( $q.$get().defer() );

不起作用。有任何想法吗?

4

1 回答 1

1

代替:

this.$get = function()
  {
    return this;
  }

试试这个:

return {
  // object being returned has to implement $get method
  $get: function() {
    // you should be able to use $q and $rootScope here
    return this;
  }
};

此外$get,应该有其他方法也可以访问所有注入的服务:

return {
  // object being returned has to implement $get method
  $get: function() {
    // you should be able to use $q and $rootScope here
    return this;
  },
  // you can also have more methods here
  someOtherMethod: function() {
    // you should be able to use $q and $rootScope here as well
    // for example:
    $rootScope.$apply();
  }
};

那是假设您注入$q$rootScope构造函数(无论您是否使用数组表示法)。

这就是我从这里这里推断出来的

于 2013-08-05T02:10:36.277 回答