所以我有一些像这样的代码:
if(result.data.authenticated === false) {
//need to send to unauthoirzed page.
$location.path('/unauthorized');
}
当它点击时,我看到我的网址变为:
/#/unauthorized
但它没有显示我在此处定义的HTML页面:
.when('/unauthorized', {
templateUrl:'partials/unauthorized.html',
controller:'unauthorizedController'
})
只停留在原始页面上。
答案 0 :(得分:0)
这是带有授权的AngularJs路由的完整示例。希望这可以帮助你
(function() {
'use strict';
var app = angular.module('app', [
'ngRoute',
]);
window.routes = {
'/': {
templateUrl: 'app/visitor/visitor.html',
anonymous: true
},
'/index': {
templateUrl: 'app/visitor/visitor.html',
caseInsensitiveMatch: true,
anonymous: true
},
'/lu/:mt?/:tab?/:id?': {
templateUrl: 'app/user/user.html',
caseInsensitiveMatch: true,
anonymous: false
}
};
app.config(function($routeProvider, $locationProvider) {
for (var path in window.routes) {
$routeProvider.when(path, window.routes[path]);
}
$routeProvider.otherwise({
redirectTo: '/'
});
$locationProvider.html5Mode(true);
});
app.config(function($routeProvider, $locationProvider) {
for (var path in window.routes) {
$routeProvider.when(path, window.routes[path]);
}
$routeProvider.otherwise({
redirectTo: '/'
});
$locationProvider.html5Mode(true);
});
app.run(function($rootScope, toastr, sessionService, $location) {
$rootScope.$on('$routeChangeStart', function(event, next, current) {
for (var i in window.routes) {
if (next && next.hasOwnProperty('$$route') && next.$$route.hasOwnProperty('originalPath') &&
next.$$route.originalPath.localeCompare(i) == 0) {
if (!window.routes[i].anonymous && !sessionService.getUserAuthenticated()) {
toastr.error('Access not found');
event.preventDefault();
$location.path('/');
} else if (window.routes[i].anonymous && sessionService.getUserAuthenticated()) {
event.preventDefault();
$location.path('/aa/xx/ff');
}
}
}
});
});
})();

答案 1 :(得分:-1)
测试路由是否成功更新,在unauthorizedController
控制器添加日志或调试器中测试路由器是否更改,并使用templateUrl
重新template
并查看结果