angular ui router - AngularJS - Dynamic $StateProvider -
i eagerly trying create 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 array brackets in resolve navigation not work. // array brackets, once sales clicked get: // error: [ng:areq] argument 'fn' not function, got object
the 2 methods below work long not using resolve:
works: angular.foreach(states, function(state) { $stateprovider.state(state); }); works: angular.foreach(states, function(state) { $stateprovider.state(state.name, state); });
question: how can make dynamic $stateprovider work resolve?
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); });
Comments
Post a Comment