Why does this give me an error:
为什么这会给我一个错误:
angular.module('app')
.config(function($routeProvider, $locationProvider, $httpProvider, $location) {
Uncaught Error: Unknown provider: $location from app
未捕获错误:未知提供商:来自应用的$ location
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?
这是相同的应用程序。可以配置只获取提供商和工厂只获得非提供商?
2 个解决方案
#1
30
Only providers and constants may be injected into configuration blocks.
只有提供者和常量可以注入配置块。
From the angularjs documentation on configuration blocks
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
配置块 - 在提供程序注册和配置阶段执行。只有提供程序和常量才能注入配置块。这是为了防止在完全配置服务之前意外实例化服务
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
});
#2
7
there are two module level ways to inject code:
注入代码有两种模块级方法:
1) config
. This method will run before the injectors are created, and only accepts providers and constants for injection
1)配置。此方法将在创建注入器之前运行,并且仅接受用于注入的提供者和常量
2) run
. This method will run during the app initialization phase and accepts only instances and constants (such as $location
).
2)跑。此方法将在应用程序初始化阶段运行,并仅接受实例和常量(例如$ location)。
factory
(and service
, controller
, and directive
) are functions which are part of your application. As such they too can only accepts instances(or singletons) and constants.
factory(以及service,controller和directive)是应用程序的一部分。因此,它们也只能接受实例(或单例)和常量。
#1
30
Only providers and constants may be injected into configuration blocks.
只有提供者和常量可以注入配置块。
From the angularjs documentation on configuration blocks
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
配置块 - 在提供程序注册和配置阶段执行。只有提供程序和常量才能注入配置块。这是为了防止在完全配置服务之前意外实例化服务
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
});
#2
7
there are two module level ways to inject code:
注入代码有两种模块级方法:
1) config
. This method will run before the injectors are created, and only accepts providers and constants for injection
1)配置。此方法将在创建注入器之前运行,并且仅接受用于注入的提供者和常量
2) run
. This method will run during the app initialization phase and accepts only instances and constants (such as $location
).
2)跑。此方法将在应用程序初始化阶段运行,并仅接受实例和常量(例如$ location)。
factory
(and service
, controller
, and directive
) are functions which are part of your application. As such they too can only accepts instances(or singletons) and constants.
factory(以及service,controller和directive)是应用程序的一部分。因此,它们也只能接受实例(或单例)和常量。