配置中的服务注入AngularJS
Service injection in config. AngularJS
我有这项服务:
app.service("UserService", function(){
var userService = {};
userService.userInfo = [
{firstName: "John", lastName: "Doe"},
{firstName: "Carl", lastName: "Smith"}
];
return userService;
});
我想将其注入 .config 以定义路由(为此我使用 ui-router)
app.config(["userService", function($stateProvider, userService){
$stateProvider
.state("#",{
templateUrl: "index.html",
controller: "UserListController"
})
.state('users', {
url: '/user/:username',
templateUrl: "view/userProfile.html",
controller: "UserListController",
params: { username: userService.userInfo[0].firstName}
})
...
这无法正常工作。关于我可以将数据注入该服务以供 .config 访问的任何想法吗?
谢谢:)
对于 AngularJS 中的注入,如果您要提供要注入的名称,则需要提供所有名称,并以与声明它相同的方式提供。
在这种情况下:
app.config(["$stateProvider", "UserService", function($stateProvider, userService){
在配置过程中,您只能访问提供程序,在您的情况下,您可以通过将其注入 app.config
来访问 UserServiceProvider
,如下所示:
app.config(function(UserServiceProvider){
// Config stuffs...
})
通过此提供商,您可以通过 $get()
访问您的服务 属性,例如:
UserServiceProvider.$get().userInfo[0]
Here 是一个工作的 plunker
常量在配置阶段和 运行 阶段都是可注入的:
app.constant("UserConstant", {
userInfo:[
{firstName: "John", lastName: "Doe"},
{firstName: "Carl", lastName: "Smith"}
]
});
来自文档:
constant(name, value);
Register a constant service with the $injector
, such as a string, a number, an array, an object or a function. Like the value, it is not possible to inject other services into a constant.
But unlike value, a constant can be injected into a module configuration function (see angular.Module) and it cannot be overridden by an AngularJS decorator.
我有这项服务:
app.service("UserService", function(){
var userService = {};
userService.userInfo = [
{firstName: "John", lastName: "Doe"},
{firstName: "Carl", lastName: "Smith"}
];
return userService;
});
我想将其注入 .config 以定义路由(为此我使用 ui-router)
app.config(["userService", function($stateProvider, userService){
$stateProvider
.state("#",{
templateUrl: "index.html",
controller: "UserListController"
})
.state('users', {
url: '/user/:username',
templateUrl: "view/userProfile.html",
controller: "UserListController",
params: { username: userService.userInfo[0].firstName}
})
...
这无法正常工作。关于我可以将数据注入该服务以供 .config 访问的任何想法吗?
谢谢:)
对于 AngularJS 中的注入,如果您要提供要注入的名称,则需要提供所有名称,并以与声明它相同的方式提供。
在这种情况下:
app.config(["$stateProvider", "UserService", function($stateProvider, userService){
在配置过程中,您只能访问提供程序,在您的情况下,您可以通过将其注入 app.config
来访问 UserServiceProvider
,如下所示:
app.config(function(UserServiceProvider){
// Config stuffs...
})
通过此提供商,您可以通过 $get()
访问您的服务 属性,例如:
UserServiceProvider.$get().userInfo[0]
Here 是一个工作的 plunker
常量在配置阶段和 运行 阶段都是可注入的:
app.constant("UserConstant", {
userInfo:[
{firstName: "John", lastName: "Doe"},
{firstName: "Carl", lastName: "Smith"}
]
});
来自文档:
constant(name, value);
Register a constant service with the
$injector
, such as a string, a number, an array, an object or a function. Like the value, it is not possible to inject other services into a constant.But unlike value, a constant can be injected into a module configuration function (see angular.Module) and it cannot be overridden by an AngularJS decorator.