Reputation: 360
I want to inject service into my .run
block, but when I inject it I cannot use them.
My code is :
.run(['AuthFactory', function ($state, $rootScope, AuthFactory, $location) {
console.log("Auth Factory :%O", AuthFactory);
AuthFactory.registerUserChangeHandler(function (currentUser) {
$rootScope.currentUser = currentUser;
});
AuthFactory.refresh().then(function (currentUser) {
console.log("Current User is", currentUser);
}, function (reason) {
// User is not Logged in
$location.path("login");
});
}]);
When I write this code I get error :
"app.js:120Uncaught TypeError: Cannot read property 'registerUserChangeHandler' of undefined"
If I simply inject AuthFactory
in function then everything works fine, but now I want to inject UserService
& use methods inside the service.
I tried injecting it in function but I am unable to use it.
Open for all suggestions.
Upvotes: 0
Views: 35
Reputation: 12093
Mistake is,if you use array way of dependency injection , than you need to follow the sequence. For example as $state if first value in array, than inside controller callback the first value will represent $state.
.run(['$state','$rootScope','AuthFactory','$location', function ($state, $rootScope, AuthFactory, $location) {
console.log("Auth Factory :%O", AuthFactory);
// UserService.login({
// 'username': "guest",
// 'password': "guest"
// }, function () {
// $state.go("corporate_site.home", {reload:'true'});
// }, function () {
// $rootScope.error = "Login Failed. Invalid Credentials.";
// });
// $state.go("corporate_site.home", {reload: 'true'});
AuthFactory.registerUserChangeHandler(function (currentUser) {
$rootScope.currentUser = currentUser;
});
AuthFactory.refresh().then(function (currentUser) {
console.log("Current User is", currentUser);
}, function (reason) {
// User is not Logged in
$location.path("login");
});
}]);
Upvotes: 0
Reputation: 701
You mess up in DI.
.run(['state', '$rootScope', 'AuthFactory', '$location',
function ($state, $rootScope, AuthFactory, $location) {
console.log("Auth Factory :%O", AuthFactory);
// ...
}]);
Upvotes: 2