http://stackoverflow.com/questions/11541695/redirecting-to-a-certain-route-based-on-condition
'use strict';
var app = angular.module('app', [])
.config(['$routeProvider', function($routeProvider) {
$routeProvider
.when('/', {
templateUrl: "login.html",
controller: LoginController
})
.when('/private', {
templateUrl: "private.html",
controller: PrivateController,
resolve: {
factory: checkRouting
}
})
.when('/private/anotherpage', {
templateUrl:"another-private.html",
controller: AnotherPriveController,
resolve: {
factory: checkRouting
}
})
.otherwise({ redirectTo: '/' });
}]);
var checkRouting= function ($q, $rootScope, $location) {
if ($rootScope.userProfile) {
return true;
} else {
var deferred = $q.defer();
$http.post("/loadUserProfile", { userToken: "blah" })
.success(function (response) {
$rootScope.userProfile = response.userProfile;
deferred.resolve(true);
})
.error(function () {
deferred.reject();
$location.path("/");
});
return deferred.promise;
}
};
=========================angular.module('yoApp')
.service('appService', function ($q, $timeout, $rootScope, $location) {
this.isAdmin=function(){
console.log("isAdmin");
var deferred = $q.defer();
deferred.resolve(true);
// deferred.reject();
// $location.path("/about");
return deferred.promise;
}
});
.config(function ($routeProvider) {
$routeProvider
.when('/me', {
template: '<h1> me </h1>',
resolve: {
factory: function(appService) {
return appService.isAdmin();
}
}
})
});
沒有留言:
張貼留言