Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I need to construct permission based admin dashboard. Header of html consists of some main havs and the last nav is for Settings. I use angular-ui-router for states. and when user goes to the Settings link I need to upload from server tabs that user has access to.

My state provider

 $stateProvider
    .state('home', {
        url: "/",
        templateUrl: "templates/home/index.html"
    })
    .state('settings', {
        url: "/settings",
        controller: "SettingsController",
        templateUrl: "templates/home/settings.html",
    })
    .state('settings.users', {
        url: "/users",
        controller: "SettingsUsersController",
        templateUrl: "templates/home/settings/users.html",
    })
    .state('settings.roles', {
        url: "/roles",
        controller: "SettingsRolesController",
        templateUrl: "templates/home/settings/roles.html",
    });

SettingsController looks like this:

app.controller('SettingsController', ['$scope', '$http',
    function($scope, $http) {
        $scope.tabs = [];

        $http({
            method: 'GET',
            url: '/api/menu/get'
        }).
        success(function(data, status, headers, config) {
            $scope.tabs = data;
        }).
        error(function(data, status, headers, config) {
            window.location = '/auth/login';
        });
    }
]);

Sample server answer

[{
    "url": "settings.users",
    "title": "Users"
}, {
    "url": "settings.roles",
    "title": "Roles"
}]

My settings.html

<div class="container">
  <ul class="nav nav-tabs nav-justified">
    <li ng-repeat="tab in tabs" ng-class="{active: $first}">
      <a href="{{tab.url}}">{{tab.title}}</a>
    </li>
  </ul>
</div>

How to load first tab on $http end in SettingsController, also to connect it to corresponding Controller? And how to switch active tab on different states?

share|improve this question

1 Answer 1

up vote 0 down vote accepted

Don't know if it is possible to define routes dynamically, but you could achieve your targets the following way.

  1. Define all possible routes of your app.
  2. Make them inaccessible for user without certain permissions (i use this approach).
  3. Load user's permissions.
  4. Build tabs from those permissions (don't show nav before this point).

Additionally, it's worth checking on server side if user has permissions to page when he tries to load it. Because he can look at code and to try to access it bypassing the navigation.

share|improve this answer
    
My problem is not Auth, server is responding with states, that are allowed to user (as in my example of JSON). What I am trying to achieve is to set first tab active, load it's content and if I have another tab's url - make it active and load corresponding data. –  andrewdacenko May 23 at 7:14
1  
You can set first tab active by: 1) inject $state into your app.run() and assign it to the $rootScope: app.run(function($rootScope, $state, ...){ $rootScope.$state = $state; }) to make it available in controllers via $scope. 2) inside your success callback make .success(function(data){ $scope.tabs = data; $scope.$state.go($scope.tabs[0].url); }). Content of this state will be loaded inside it's controller or via 'resolve' property of ui-router state. –  gorpacrate May 23 at 8:54
    
About another tabs - you want to preload their data or what? –  gorpacrate May 23 at 9:01
    
Thank you, its something what i wanted. When application runs and I click at /settings link at first it redirects me to the /settings/users, but if I click one again at /settings, SettingController doesn't redirects to the /settings/users. How to achieve that behavior? Another question: my templates/home/settings/users.html in SettingsUsersController doesn't loads anywhere, why? –  andrewdacenko May 23 at 10:19
    
Could you make jsfiddle example? It's not so obvious only in words. –  gorpacrate May 26 at 7:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.