admin管理员组文章数量:1323342
I am trying to add a resolver function to every state within my application in order to wait for an application 'boot' process to plete. I have the following code already:
angular.forEach($state.get(), function(state) {
state.resolve = state.resolve || {};
state.resolve.session = function() {
return SessionResolver.handle(state);
}
});
SessionResolver is a service where the handle(state)
function returns a promise that is resolved once the boot process is plete.
This works fine unless a state has been defined with no existing resolve
object. For example, the following state will block until the SessionResolver.handle promise resolves:
$stateProvider.state('dashboard', {
url: "/",
templateUrl: "dashboard.html",
controller: "DashboardCtrl",
resolve: {}
});
But if the resolve object isn't set the state doesn't block:
$stateProvider.state('dashboard', {
url: "/",
templateUrl: "dashboard.html",
controller: "DashboardCtrl",
});
I'd rather not add an empty resolve
object to each of my states, since I might as well add the SessionResolver.handle resolve function to them all while I'm at it.
Any ideas why dynamically adding resolve functions to states fails unless the resolve object already exists?
I am trying to add a resolver function to every state within my application in order to wait for an application 'boot' process to plete. I have the following code already:
angular.forEach($state.get(), function(state) {
state.resolve = state.resolve || {};
state.resolve.session = function() {
return SessionResolver.handle(state);
}
});
SessionResolver is a service where the handle(state)
function returns a promise that is resolved once the boot process is plete.
This works fine unless a state has been defined with no existing resolve
object. For example, the following state will block until the SessionResolver.handle promise resolves:
$stateProvider.state('dashboard', {
url: "/",
templateUrl: "dashboard.html",
controller: "DashboardCtrl",
resolve: {}
});
But if the resolve object isn't set the state doesn't block:
$stateProvider.state('dashboard', {
url: "/",
templateUrl: "dashboard.html",
controller: "DashboardCtrl",
});
I'd rather not add an empty resolve
object to each of my states, since I might as well add the SessionResolver.handle resolve function to them all while I'm at it.
Any ideas why dynamically adding resolve functions to states fails unless the resolve object already exists?
Share Improve this question asked Jun 11, 2014 at 11:40 Ross WilsonRoss Wilson 1297 bronze badges2 Answers
Reset to default 9I've solved the issue in this way:
angular.module('sample', ['ui.router']).config(function($stateProvider) {
var $delegate = $stateProvider.state;
$stateProvider.state = function(name, definition) {
definition.resolve = angular.extend({}, definition.resolve, {
myCustomResolve: function() { return true; }
});
return $delegate.apply(this, arguments);
};
$stateProvider.state('example', { url: 'example/' });
}).run(function($state) { console.log('have resolve?', $state.get('example')); });
I may not pletely understand the context for you issue, but it seems what you are looking for is an abstract state. You can declare a base abstract state for your app that all other states will be a child of, and add the resolve into that abstract state. That way all states will "block" until the parent resolve block is finished.
$stateProvider.state('parent', {
abstract: true,
template: '',
resolve: {
session: function() {
return SessionResolver.handle(state);
}
}
}).state('parent.child1', {
url: '/',
template: 'dashboard.html',
controller: 'DashboardController'
});
You can find more documentation about abstract states here https://github./angular-ui/ui-router/wiki/Nested-States-%26-Nested-Views#abstract-states.
本文标签: javascriptAngular uirouter dynamically add resolve function to all statesStack Overflow
版权声明:本文标题:javascript - Angular ui-router dynamically add resolve function to all states - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742134729a2422319.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论