我想將服務注入到我的.run
塊中,但注入時我無法使用它們。無法在角度js中的.run中注入服務
我的代碼是:
.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");
});
}]);
當我寫這篇文章的代碼,我得到錯誤:
"app.js:120Uncaught TypeError: Cannot read property 'registerUserChangeHandler' of undefined"
如果我只是在功能上則一切正常注入AuthFactory
,但現在我想注入UserService
&使用服務內部的方法。
我試着在功能上注入它,但我無法使用它。
對所有建議開放。