为什么一定要一个提供商配置块之前定义 [英] Why must a provider be defined before a config block

查看:175
本文介绍了为什么一定要一个提供商配置块之前定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模块。它有一个配置块,供应商,以及定义的常量。配置块同时引用常数和供应商。我注意到,我不断前或我的配置块之后进行定义。提供者必须然而,配置块之前定义,否则我得到一个错误。

I have a module. It has a config block, a provider, and a constant defined. The config block references both the constant and the provider. I notice that my constant can be defined before or after my config block. The provider however must be defined BEFORE the config block or else I get an error.

Error: [$injector:modulerr] Failed to instantiate module loadOrder due to:
[$injector:unpr] Unknown provider: greetingsProvider

下面是一些示例code:

Here is some sample code:

var myModule = angular.module('loadOrder', []);

//if I define this after the config block, I get an error
angular.module('loadOrder').provider('greetings',[ function(){
    this.$get = [function(){
        return { greet: function(){ return "Hola"; } };
    }];
}]);

myModule.config(['$provide', 'greetingsProvider', 'planetName', function($provide, loadOrderProvider, planetName){
    $provide.value('someVals',[3,6,8]);
    console.log("Lets go to", planetName);
}]);

myModule.constant('planetName', 'Saturn');

这是为什么?为什么我不能确定我的供应商我的配置块后?

Why is this? Why can't I define my provider after my config block?

推荐答案

当你调用提供商配置没有立即发生。该呼叫登记,放在一个队列和运行应用程序的初始化过程中。

When you call provider, config or constant nothing happens immediately. The calls are registered, put in a queue and run during the initialization of the application.

有趣的恒的是,它是在队列的前面放。因此,它的配置前可用,不管是什么。

The funny thing with constant is that it is put at the front of the queue. Thus it's available before config, no matter what.

这篇关于为什么一定要一个提供商配置块之前定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆