问题描述
我在我的项目中使用UI路由器。我的应用程序的主页包含4个选项卡,每个选项卡都路由到不同的模板。这是我目前的路由代码,即使用forEach创建6条路由。
I'm using UI router in my project. The home page of my application consists of 4 tabs, each routing to a different template. This is my current routing code, im using a forEach to create 6 routes.
['Draft','Assigned','InProgress','Completed','Rejected','All'].forEach(function (val) {
$stateProvider.state({
name: 'root.jobs.list.' + val.toLowerCase(),
url: '/' + val.toLowerCase(),
views: {
'currentTab': {
templateUrl: 'adminworkspace/jobs',
controller: 'JobsController'
}
},
data: {
userJobsStatus: val
}
});
});
默认情况下,当用户登录时,它会转到root.jobs.list.draft。如何根据登录用户的角色(管理员,用户,文员等)重定向到给定状态。如果要将属于工程师或首席工程师角色的所有用户重定向到root.jobs.list.inprogress
By default when the user logs in, it goes to "root.jobs.list.draft". How do redirect to a given state based on the logged in user's role (Admin, User, Clerk etc..). If want to redirect all users that are part of the "Engineer" or "Lead Engineer" role to "root.jobs.list.inprogress"
我最初在控制器中有这个,但正如你所看到的,它不起作用,因为每次我点击一个标签时,它总是路由回root.jobs.list.inprogress
I originally had this in the controller, but as you can see, it didn't work, because each time I clicked on a tab, it always routes back to "root.jobs.list.inprogress"
if (user !== undefined) {
if (user.BusinessRole == "Engineer" || user.BusinessRole == "Lead Engineer")
$state.go('root.jobs.list.inprogress');
}
推荐答案
我已经不得不解决那个:
I already have had to solve that :
我已经注册了一个仅用于根据角色处理默认页面的状态。
I've registered a state that was used only to handle the default page based on the role.
$urlRouterProvider.otherwise("/");
$stateProvider.state("default", {
url:"/",
templateUrl: "app/core/home/default.html",
controller: "DefaultController"
});
控制器只是:
(function () {
"use strict";
angular.module("core").controller("DefaultController", [
"$rootScope",
"$state",
"roles",
function ($rootScope, $state, roles) {
if ($rootScope.hasPermission(roles.SomeRoleName)) {
$state.go("someStateName");
} else if ($rootScope.hasPermission(roles.SomeRoleName)) {
$state.go("someStateName");
}
}
]);
})();
这篇关于ui-router:基于用户角色的默认路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!