Skip to content Skip to sidebar Skip to footer

AngularJS - How Do I Get The State Name Inside Of State Resolve Function To Load Controller Files?

I am making head ways into diving into my first complete AngularJS app using PHP and tailored toward an api-centric approach. I have reached this point: I want to be able to captur

Solution 1:

$state.current has all the information about the current state, including the name. So $state.current.name will get you the information you need.


Solution 2:

Just keep the code simple:

 $stateProvider
    .state('action', {
        name: 'action', //<--state name I want to capture for this url 
        url: "/actionitems",
        resolve: {
            loadDependencies: function($ocLazyLoad) {
                return $ocLazyLoad.load("action");
            }
        },
        templateUrl: '/app/tool/action/ActionItems.html'
 });

Solution 3:

I added the allowed method to the resolve section and cleaned up the code to get the desired outcome. I declared a global state to capture the value in $state$.name

var state = '';

//route-config.js
function load($ocLazyLoad, $q)
{
    var deferred = $q.defer();
    try
    {
        $ocLazyLoad.load(state).then(function ()
        {
            deferred.resolve();
        });
    }
    catch (ex)
    {
        deferred.reject(ex);
    }
    return deferred.promise;
}


function configRoutes($stateProvider, $urlRouterProvider, $ocLazyLoadProvider)
{

    var res = 
    {
        loadDependencies: ['$ocLazyLoad', '$q', load],
        allowed: function ($state$)
        {
            state = $state$.name;
        }
    };


    $urlRouterProvider
        .when('action', 'action')
        .when('issue', 'issue')
        .when('lesson', 'lesson')
        .when('opportunity', 'opporutnity')
        .when('risk', 'risk')
        .otherwise('main');


    $ocLazyLoadProvider.config(
    {
        modules: [
        {
            name: 'action',
            files: ['app/tool/action/ActionController.js']
        },
        {
            name: 'risk',
            files: ['app/tool/risk/RiskController.js']
        }]
    });


    $stateProvider
       .state('action',
        {
            url: "/actionitems",
            resolve: res,
            templateUrl: '/app/tool/action/ActionItems.html'
        });

    $stateProvider
        .state('risk',
        {
            url: "/risks",
            resolve: res,
            templateUrl: '/app/tool/risk/Risks.html'
        });
}

Post a Comment for "AngularJS - How Do I Get The State Name Inside Of State Resolve Function To Load Controller Files?"