Question

I am eagerly trying to create a dyanmic $StateProvider:

Standard static $StateProvider:

    $stateProvider
      .state('main',
      {
        url: '/',
        templateUrl: '/views/main.html',
        controller: 'MainCtrl'
      })
      .state('login',
      {
        templateUrl: '/views/login-form.html',
        controller: 'LoginCtrl'
      })
      .state('logout',
      {
        controller: 'LogoutCtrl'
      })
      .state('sales', {
        templateUrl: '/views/sales-data.html',
        controller: 'SalesDataCtrl',
        resolve: {
          user: 'User',
          authenticationRequired: function(user) {
            user.isAuthenticated();
          }
        }
      });

Dynamic $StateProvider:

var verify = function(user) { user.isAuthenticated(); };

var states= [
{ name: 'main', url: '/', templateUrl: '/views/main.html', controller: 'MainCtrl' },
{ name: 'login', url: '', templateUrl: '/views/login-form.html', controller: 'LoginCtrl' },
{ name: 'logout', url: '', templateUrl: '', controller: 'LogoutCtrl' },
    {
        name: 'sales',
        url: '',
        templateUrl: '/views/sales-data.html',
        controller: 'SalesDataCtrl',
        //resolve:
        //[
        //    { user: 'User', authenticationRequired: 'verify();' }
        //]
    }
];
       // w/o the array brackets in resolve the navigation does not work.
       // with the array brackets, once sales is clicked I get:
       // Error: [ng:areq] Argument 'fn' is not a function, got Object

The two methods below work as long as I am not using resolve:

  This works:
   angular.forEach(states, function(state) {
        $stateProvider.state(state);
    });

   This works:
   angular.forEach(states, function(state) {
        $stateProvider.state(state.name, state);
    });

Question: How can I make this dynamic $StateProvider work with the resolve?

Was it helpful?

Solution

Solution:

var states = [
{ name: 'main', url: '/', templateUrl: '/views/main.html', controller: 'MainCtrl' },
{ name: 'login', url: '', templateUrl: '/views/login-form.html', controller: 'LoginCtrl' },
{ name: 'logout', url: '', templateUrl: '', controller: 'LogoutCtrl' },
    {
        name: 'sales',
        url: '',
        templateUrl: '/views/sales-data.html',
        controller: 'SalesDataCtrl',
        resolve: {
            user: 'User',
            authenticationRequired:
            ['user', function(user) { user.isAuthenticated(); }] // <-------------------------
        }
    }
];

    angular.forEach(states, function (state) {
        $stateProvider.state(state.name, state);
    });

OTHER TIPS

I think you're calling $stateProvider.state(...) incorrectly, it takes a state ID and then the configuration object.

Assuming your name properties are valid and unique you should just be able to do this (or adjust it as needed):

angular.forEach(states, function(state) {
    $stateProvider.state(state.name, state);
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top