0
我有一個需要訪問服務的控制器。如果我只是簡單地將服務作爲參數添加到我的控制器的構造函數中,它的工作原理很好,但是如果我嘗試使用$ inject註釋參數,以便稍後將JavaScript最小化時我沒有問題,例外: 「無法讀取屬性'用戶'未定義」
一切仍然正常,但我不明白異常來自哪裏。
用於服務的代碼是:
var myAppServices = angular.module('myApp.services', []);
myAppServices.factory('User', function(){
var user = {
name:"anonymous",
id: -1,
authorized:false
};
return user;
});
而且控制器看起來是這樣的:
function MainController($rootScope, $location, User){
$rootScope.$on("$routeChangeStart", function (event, current, previous){
console.log(User);
if(User.authorized === false && current.templateUrl !== 'partials/partial1.html'){
console.log("returning false")
$location.path('/view1');
return false;
}
});
}
MainController.$inject['$rootScope', '$location', 'User'];
拋出異常的代碼上面對$注入註釋的最後一行被稱爲。
跌宕!我真的需要得到更多的睡眠;-) –