1

in the below example i have variable called roles inside data for this variable i have to add array of roles. now i in the second state have predefined the roles but this role array i have to take load from the web api.

.config(['$stateProvider', '$urlRouterProvider', function ($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('signin');
$stateProvider.state('site', {
    'abstract': true,
    resolve: {
        authorize: ['authorization', function (authorization) {
            alert(JSON.stringify(authorization))
            return authorization.authorize();
        }, ]
    }
})
.state('signin', {
    parent: 'site',
    data: {
        roles: []
    },
    url: '/signin',
    views: {
        '': { templateUrl: '/signin.html' },
        'content@': {
            templateUrl: '/index.html',
            controller: 'SigninCtrl'
        }
    }
})

.state('WorkArea', {
   parent: 'site',
   url: '/WorkArea',
   data: {
       roles: ['User', 'Dev']
   },
   views: {
       '': { templateUrl: '/Views/WorkArea/WorkArea.html' },
       'ContentOne@': {
           templateUrl: '/Views/WorkArea/ProjectList.html',
           controller: 'ProjectController'
       }
   }
 })
}]);

i am referring this example from below link:-

angular ui-router login authentication

Community
  • 1
  • 1
Raj
  • 31
  • 9

1 Answers1

0

Define it in this way:

 .state('WorkArea', {
   parent: 'site',
   url: '/WorkArea',
   data: {
       roles: (function() {
                return ['User', 'Dev'];
              })()
   },
 })

You can use IIFE for this case to dynamically add properties to data attribute.

In your app.run use:

$rootScope.$on('$stateChangeStart', function(event, toState){ 
    var roles = toState.data.roles ;
    console.log(roles);
   // your custom logic here for that state
})
mohamedrias
  • 18,326
  • 2
  • 38
  • 47
  • if i am adding `roles` to resolve then i am getting error like roles is undefined because i am checking condition like ` if ($rootScope.toState.data.roles && $rootScope.toState.data.roles.length > 0 && !principal.isInAnyRole($rootScope.toState.data.roles)) { }` – Raj Mar 07 '15 at 11:18