Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在AngularJs中设置服务提供者对服务的依赖关系?_Javascript_Angularjs - Fatal编程技术网

Javascript 如何在AngularJs中设置服务提供者对服务的依赖关系?

Javascript 如何在AngularJs中设置服务提供者对服务的依赖关系?,javascript,angularjs,Javascript,Angularjs,我正在努力做到以下几点: var myApp = angular.module('myApp', []); myApp.provider('routeResolver', ['$q','$rootScope',function($q, $rootScope) { console.log( $q, $rootScope ); this.$get = function() { return this;

我正在努力做到以下几点:

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

);
myApp.provider('routeResolver', ['$qProvider','$rootScopeProvider',function($q, $rootScope)
    {        
        console.log( $q.defer() );
        console.log( $rootScope );
        this.$get = function()
        {
            return this;
        }
    }]
);
但是,这会产生错误:
未知提供程序$q

因此,我将代码更改为以下内容:

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

);
myApp.provider('routeResolver', ['$qProvider','$rootScopeProvider',function($q, $rootScope)
    {        
        console.log( $q.defer() );
        console.log( $rootScope );
        this.$get = function()
        {
            return this;
        }
    }]
);
但是,这会产生
uknown函数的错误。
。甚至做:

console.log( $q.$get().defer() );
不起作用。有什么想法吗?

而不是:

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
注入构造函数(无论您是否使用数组表示法)


这就是我从中推断出来的,我如何从新的
$get
方法中访问
$q
$rootScope
?如果您将它们作为
$q
$rootScope
注入,则与您通常在其他任何地方(指的是“$q”和“$rootScope”)一样。但是它需要在
$get
函数中完成。我不确定你所说的“我通常在其他地方会如何”是什么意思。。我对棱角有点陌生。能否添加一段代码,说明如何从新的
$get
访问
$q
$rootScope
?如果可能的话,我希望使用数组方法来完成,这样缩小就不会成为一个问题。我自己也不确定,但我会用一些想法更新我的示例。顺便说一句,数组表示法被视为与传递函数相同,具有您提到的好处。那应该没什么区别。