3

I want to route based on a user type in my angular app.

So if user is Admin, then should be redirected to admin.html and if user then user.html based on the value coming of user type from database after login.

In this I want to add condition:

.when('/dashboard', {        
    title: 'dashboard',
    templateUrl: 'partials/User.html',
    controller: 'authCtrl'    
})
DAXaholic
  • 33,312
  • 6
  • 76
  • 74

3 Answers3

0

I did it using ui-router and not ng-router, but you can still check my answer here:

Check if user has permission to access specific states

Basically I build a service and validated if user has access to that state else, redirect the user to another state.

The service was being called in the run function on every state change.

Community
  • 1
  • 1
Rahul Arora
  • 4,503
  • 1
  • 16
  • 24
0

In your login controller inject the $routeParams dependency, then affect to a $routeParams variable the value of user ( simple user or admin ) like this

$routeParams.user = data.user;

Now in configure your route like this

.when('/:user', {
      title: 'dashboard',
      templateUrl:'partials/:user.html'
      controller: 'authCtrl'  
 });

so if it's simple user you are going to be redirected to partials/user.html and if it's the admin you are going to be redirected to partials/admin.html .

Abdennacer Lachiheb
  • 4,388
  • 7
  • 30
  • 61
0

you could use .run function and check the user there and redirect to conditional page using $location.

.when('/user', {        
    title: 'dashboard',
    templateUrl: 'partials/User.html',
    controller: 'authCtrl'    
}).when('/admin', {        
    title: 'dashboard',
    templateUrl: 'partials/Admin.html',
    controller: 'authCtrl'    
})

from run function

.run(function($location){
    console.log("run")
    var user = "user";
    if(user === "Admin"){
      $location.path("/admin")
    }else{
       $location.path("/user")
    }
  })

from controller

.controller('authCtrl',function($location){
    console.log("controller")
     var user = "admin";
    if(user === "admin"){
      $location.path("/admin")
    }else{
       $location.path("/user")
    }
  })
Jorawar Singh
  • 7,463
  • 4
  • 26
  • 39
  • $routeParams.user = Data.usertype; if($routeParams.user.equals('U')) { $location.path('user'); } else{ $location.path('dashboard'); } – gaurav singh Jul 31 '16 at 11:52
  • Yes it should work but if you want it before controller loading then you can use angular.run function and redirect from there. – Jorawar Singh Jul 31 '16 at 12:04
  • Need to do through controller itself but in controller error is coming her is Code : – gaurav singh Jul 31 '16 at 13:14
  • app.controller('authCtrl', function ($scope, $rootScope, $routeParams, $location, $http, Data) { //initially set those objects to null to avoid undefined error $scope.login = {}; $scope.signup = {}; $scope.doLogin = function (customer) { Data.post('login', { customer: customer }).then(function (results) { Data.toast(results); – gaurav singh Jul 31 '16 at 13:14
  • if (results.status == "success") { $routeParams.user = Data.usertype; if($routeParams.user.equals('U')) { $location.path('user'); } else{ $location.path('dashboard'); } //$location.path('dashboard'); } }); }; – gaurav singh Jul 31 '16 at 13:14
  • Almost got it you tell me how i can use this:$routeParams.user.equals('U') Its saying equals is not a method.How can i perform this test? – gaurav singh Jul 31 '16 at 13:19
  • there is no **equals** **function** in **javascript** as it exists in other languages like **java** you can do like this routeParams.user === "U" – Jorawar Singh Jul 31 '16 at 13:21
  • At last it worked.As ia am newbie in Angular so took some time Thanks Jorawar – gaurav singh Jul 31 '16 at 13:26
  • Good you are welcome.. and let me know if you need help in future :-) – Jorawar Singh Jul 31 '16 at 13:27
  • Sure Jorawar. I will ask for help when needed – gaurav singh Jul 31 '16 at 13:29
  • Jorawar how can i achieve nested routing?means in user.html if i have three sections user/a user/b user/c? – gaurav singh Aug 01 '16 at 18:35
  • You mean same page just navigating to different section? feels wrong site to chat :-) my fb id SinghJorawar1010 – Jorawar Singh Aug 01 '16 at 18:38