Roles in Angular JS

Asked

Viewed 67 times

0

Well, not found in any other post, if there is srry :) at last,

I have the following config

myApp.config(function($routeProvider) {
    $routeProvider

        // route for the index page
        .when('/index', {
            templateUrl : 'pages/index.html',
            controller  : 'mainController'
        })

        // route for the home page
        .when('/home', {
            templateUrl : 'pages/home.html',
            controller  : 'aboutController'
        })

}); 

only that he wanted to work with different views, for different users, on the same route.

For example, I have two users, the X user who is Adm and the Y user who is an ordinary user, so when they both access the '/index' route they will have the same view.

But when accessing the route '/home' I wanted a different template to be shown for each one. How can I do this?

I accept all kinds of help. VALEEU!

1 answer

1


You can create directives and insert them into your home.html

In the controller aboutController Voce captures session data, checks whether the user is logged in and places the information in the $scope.usuario. You can pass it to the directive and use the object usuario there.

home html.

<div ng-if="usuario.privilegio == 'guest'">
<diretiva-para-guest usuario=usuario></diretiva-para-guest>
</div>

<div ng-if="usuario.privilegio == 'admin'">
<diretiva-para-admin usuario=usuario></diretiva-para-admin>
</div>

In the directive you call the Htmls for each and pass the data as you want...

directive-guest.js

app.directive('diretivaParaGuest', function() {

    return {
        restrict: 'E',
         scope: {
            usuario: '='

        },
        templateUrl: "home-guest.html",
        controller: function($scope){


          $scope.ola = "Hello Usuario" + $scope.usuario.nome;

         },
        controllerAs: 'guestController'
    };
});

js-admin.

app.directive('diretivaParaAdmin', function() {

    return {
        restrict: 'E',
        scope: {
        usuario: '='

    },
        templateUrl: "home-admin.html",
        controller: function($scope){


          $scope.ola = "Hello Usuario" + $scope.usuario.nome;

         },
        controllerAs: 'adminController'
    };
});
  • Look, it made a lot of sense to me. kkkk Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.