为什么我不能得到我的配置注入$位置()? [英] Why can't I get a $location injected into in my config()?

查看:117
本文介绍了为什么我不能得到我的配置注入$位置()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这个给我一个错误:

  angular.module(应用)。配置(函数($ routeProvider,$ locationProvider,$ httpProvider,$位置){


  

未捕获的错误:未知提供商:从应用$位置


但是,这行不?

  angular.module(应用程序)。工厂(SomeResource功能($ Q $资源,$ HTTP,$位置的AuthenticationService,基数64){

这是相同的应用程序。可以通过配置只得到供应商和工厂只能获得非供应商?


解决方案

只有供应商和常量可以被注入到配置块。


  

从上配置块angularjs 文档


  
  

      
  1. 配置块 - 在供应商登记和配置阶段得到执行。只有供应商和常量可以被注入到配置块。这是prevent服务的意外实例,他们已经完全配置之前


  2.   
  3. 运行块 - 创建喷油器后得到执行,用于kickstart的应用程序。只有实例和常量可注入运行块。这是prevent进一步的系统配置过程中应用程序的运行时间。


  4.   

基本上的配置部分,可以配置它们提供注入控制器,服务,工厂前等。

  angular.module(MyModule的,[])。
 配置(功能(注射剂){//提供商注射器
   //这是配置块的一个例子。
   //你可以有很多这样的,只要你想。
   //只能注入提供商(不是实例)
   //进入配置块。
 })。
 运行(功能(注射剂){//例如注射器
   //这是一个运行块的一个例子。
   //你可以有很多这样的,只要你想。
   //只能注入情况下(不提供)
   //到运行块
 });

Why does this give me an error:

angular.module('app').config(function($routeProvider, $locationProvider, $httpProvider, $location) {

Uncaught Error: Unknown provider: $location from app

But this line doesn't?

angular.module("app").factory("SomeResource",   function($q, $resource, $http, $location, AuthenticationService, Base64) {

It's the same app. Can config only get providers and factory only get non-providers?

解决方案

Only providers and constants may be injected into configuration blocks.

From the angularjs documentation on configuration blocks

  1. Configuration blocks - get executed during the provider registrations and configuration phase. Only providers and constants can be injected into configuration blocks. This is to prevent accidental instantiation of services before they have been fully configured

  2. Run blocks - get executed after the injector is created and are used to kickstart the application. Only instances and constants can be injected into run blocks. This is to prevent further system configuration during application run time.

Essentially the configuration block is where you configure providers before they are injected into controller, services, factories and so on.

angular.module('myModule', []).
 config(function(injectables) { // provider-injector
   // This is an example of config block.
   // You can have as many of these as you want.
   // You can only inject Providers (not instances)
   // into the config blocks.
 }).
 run(function(injectables) { // instance-injector
   // This is an example of a run block.
   // You can have as many of these as you want.
   // You can only inject instances (not Providers)
   // into the run blocks
 });

这篇关于为什么我不能得到我的配置注入$位置()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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