我剛剛開始使用angularJS,我有一個問題: 如何訪問在templateUrl函數中使用$ rootScope定義的變量? 這裏是我的代碼:
myApp.config(['$routeProvider',
function($routeProvider, $rootScope) {
$routeProvider.
when('/', {
templateUrl: 'partials/login.html',
controller: 'loginCtrl'
}).
when('/home', {
templateUrl: function($rootScope){
console.log($rootScope.utilisateur.user.role_id);
if ($rootScope.utilisateur.user.role_id==2){
return 'partials/home.html';
}
else return 'partials/login.html';
},
controller: 'homeCtrl'
}).
otherwise({redirectTo:'/'});
}]);
它告訴我,utilisateur是不確定的。
$rootScope.utilisateur = null;
$rootScope.rapports = null;
然後在LoginCtrl:
我在索引控制器定義它
var user = Authentification.login(email,password);
user.success(function(response){
$rootScope.utilisateur = response;
console.log($rootScope.utilisateur);
$location.path('/home');
});
顯示你的代碼,你已宣佈$ rootScope – mindparse 2015-02-23 22:12:08
的utilisateur它看起來像$ rootScope是工作但utilisateur尚未確定。 – AlexMA 2015-02-23 22:12:10