我有這樣的服務:配置中的服務注入。 AngularJS
app.service("UserService", function(){
var userService = {};
userService.userInfo = [
{firstName: "John", lastName: "Doe"},
{firstName: "Carl", lastName: "Smith"}
];
return userService;
});
而且我想把它注入一個的.config定義路由(我使用的UI的路由器其)
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訪問嗎?
謝謝:)
看到這個https://stackoverflow.com/questions/15937267/inject-service-in-app-config – talentedandrew
也許它只是在你的quetion一個錯字,但你的服務名稱與開始大寫,但是當你包含它時,你忘了這個大寫 – JeanJacques
不,這不是我想的。問題是它似乎無法在params中訪問userService.userInfo [0] .firstName。如果我只是改變它爲一個字符串,它工作得很好 –