0

Is it possible to "stop" the state building based on a resolver result? For example, I have the following code:

    /**
 * @author v.lugovsky
 * created on 16.12.2015
 */


(function() {
    'use strict';

    angular.module('BlurAdmin.pages.dashboard', [])
        .config(routeConfig);

    /** @ngInject */
    function routeConfig($stateProvider) {

        $stateProvider
            .state('dashboard', {
                resolve: {
                    user: function(baSidebarService){
                        if(baSidebarService.getStorage()){
                            return
                        }
                    }
                },
                // if baSidebarService.getStorage() == true
                url: '/dashboard',
                templateUrl: 'app/pages/dashboard/dashboard.html',
                title: 'Dashboard',                
                sidebarMeta: {
                    icon: 'ion-android-home',
                    order: 0,
                },
            });
    }

})();

I would like it to resolve down, if true return was not mounted, ie, would hide the menu item, navigation, icon, etc.

georgeawg
  • 48,608
  • 13
  • 72
  • 95
Daniel Swater
  • 237
  • 1
  • 6
  • 19
  • Do you want to abort loading of the template and go to another state? OR do you want to load the template and display different results based on the returned value of the resolver function? – georgeawg May 02 '19 at 17:34
  • Look. This code corresponds to the "Dashboard" link of the menu, all the links are divided in the same way, in modules (I do not know why). I have more than 10 links, and all of them I need to apply the same rule, for example: When I load the page, I need to pick up the localStorage that is inserted after the login if the user has permission to access the Dashboard, if he does not have it, I do not show the link to that user logged in, and so with all the other links. – Daniel Swater May 02 '19 at 17:45
  • The big problem is that I can only recover the localStorage for a service, because all the modules load all at once, it is not possible to separate. I'm using BlurAdmin – Daniel Swater May 02 '19 at 17:45
  • Listen. When I ask you a question, you should answer that question. Don't inudate with other information. – georgeawg May 02 '19 at 18:17
  • My answer is this, I'm trying to be clearer as possible – Daniel Swater May 02 '19 at 20:12

1 Answers1

0

To abort a state change from a resolve function, return a rejected promise or throw a reason:

app.config(routeConfig);

/** @ngInject */
function routeConfig($stateProvider) {

    $stateProvider
        .state('dashboard', {
            resolve: {
                user: function(baSidebarService){
                    if(baSidebarService.getStorage()){
                        return
                    }
                    //ELSE abort state change
                    throw "no user";
                }
            },
            // if baSidebarService.getStorage() == true
            url: '/dashboard',
            templateUrl: 'app/pages/dashboard/dashboard.html',
            title: 'Dashboard',                
            sidebarMeta: {
                icon: 'ion-android-home',
                order: 0,
            },
        });
}
georgeawg
  • 48,608
  • 13
  • 72
  • 95
  • Thanks, I'll try and come back to post the result – Daniel Swater May 02 '19 at 15:03
  • In this example, I can not load anything. What I want is if localStorage case returns false, do not display the Dashboard link understand? – Daniel Swater May 02 '19 at 15:37
  • 1
    You asked how to *"stop" the state building*. This code does that. What do you want to happen next?: – georgeawg May 02 '19 at 16:58
  • See, I have a menu that is fully constructed and modules, I need it, after retrieving the data from the localStorage, I do a check, whether the user has access to or not for a particular link: for example I have a module called dashboar, another called justice, all are constructed in the same way, but with different permissions. From the result of the localStorage, do I hide or show the link and the route understood? There is nothing global, it's a BlurAdmin template – Daniel Swater May 02 '19 at 17:24