ユーザーがログインしていない場合にログインページにリダイレクトするようにルートを構成しようとしています。これは私のコードです:
angular.module('witBiz.services', []);
angular.module('witBiz.controllers', ['witBiz.services']);
var witBizModule = angular.module('witBiz', ['ngRoute' , 'witBiz.controllers', 'witBiz.services' ]);
witBizModule.config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/login', {templateUrl: 'resources/views/login.html', controller: 'LoginController'});
$routeProvider.when('/index', {templateUrl: 'resources/views/index.html', controller: 'IndexController'});
$routeProvider.otherwise({redirectTo: 'resources/views/index.html'});
}])
.run(['$rootScope', 'checkLogin', function($rootScope, checkLogin ,$routeProvider ) {
$rootScope.$on('$routeChangeSuccess', function () {
if (checkLogin())
$location.url("/index");
})
}])
.factory('checkLogin', function(){
return function() {
//perform real logic here
return true;
}
})
ここで、基本的にモジュールとサービスを宣言します。問題は $location が定義されていないため、エラーが発生します。このように $location を依存関係として注入しようとしましたが、未定義の注入 (injpt) が発生しました:
.run(['$rootScope', 'checkLogin', '$location', function($rootScope, checkLogin ,$location) {
$rootScope.$on('$routeChangeSuccess', function () {
if (checkLogin())
$location.url("/ciao");
})
}])
それで、「実行」メソッド内で組み込みの $location サービスをどのように使用できるのか疑問に思っています... $rootScope または checkLogin として依存関係としてそれを挿入できるのはなぜですか?!
私はAngular 1.2.0 rc2を使用しています。
ありがとう