我正在創建一個小項目,其中如果用戶角色是銷售然後登錄後,它會將用戶重定向到銷售表單頁面。 現在,如果用戶試圖通過在URL中輸入路徑訪問Sales form
頁面,並且用戶還沒有登錄,那麼我如何將用戶重定向到登錄頁面。這是我的角度部分。重定向登錄,當用戶沒有登錄或用戶嘗試訪問頁面直接從URL使用angularjs
app.controller('LoginCtrl',function($scope,$http,$window){
$scope.login = function(){
data={
email:$scope.email,
pwd:$scope.pwd
}
$http.post("widget/login.php",data).success(function(data){
console.log(data);
if(!data.success){
$scope.errorMsg="username or password is incorrect";
}
else{
$scope.role=data.role;
$scope.id=data.id;
if(data.role == "admin"){
$window.location.href="AdminPage.html";
}
else if(data.role == "Sales"){
$window.location.href="sales_form.html";
}
else if(data.role == "Account"){
$window.location.href="account_form.html";
}
else if(data.role == "Technical"){
$window.location.href="Technical_Form.html";
}
else{
$scope.errorMsg="username or password is incorrect";
}
}
});
}
});
從此我如何將用戶重定向到登錄頁面,如果用戶未登錄或嘗試直接從URL訪問頁面。 還有一件事我使用PHP作爲後端,因爲你可以在$http.post
部分看到它,所以給你的答案相應。讚賞的幫助,並提前謝謝。
或U可以試試這個插件[角權限(https://www.npmjs.com/package/angular-permission) – Iman
它看起來像你剛剛在此啓動。如何處理狀態/路由實際上並非角度合適的方法。在你太過分之前,我建議在這裏查看一下UI-Router for AngularJS:https://ui-router.github.io/ng1/。 –