AngularJS - UI路由器 - 以编程方式添加状态 [英] AngularJS - UI Router - programmatically add states
问题描述
有没有办法在模块配置后以编程方式将状态添加到$ stateProvider,例如服务?
Is there a way to programmatically add states to $stateProvider after module configuration, in e.g. service ?
要为这个问题添加更多上下文,我有一种情况可以采用两种方法:
To add more context to this question, I have a situation where I can go with two approaches:
- 尝试强制重新加载模块配置中定义的状态,问题是状态有 reloadOnSearch 设置为 false ,所以当我尝试 $ state.go('state.name',{new:param},{reload:true}); 没有任何反应,任何想法?
- try to force the reload on the state defined in the module configuration, the problem is that state has a reloadOnSearch set to false, so when I try $state.go('state.name', {new:param}, {reload:true}); nothing happens, any ideas ?
州定义
.state('index.resource.view', {
url: "/:resourceName/view?pageNumber&pageSize&orderBy&search",
templateUrl: "/resourceAdministration/views/view.html",
controller: "resourceViewCtrl",
reloadOnSearch: false,
})
- 尝试以编程方式添加我需要从服务加载的状态,以便路由可以正常工作。如果可能的话,我宁愿选择第一个选项。
推荐答案
请参阅-edit-for更新信息
通常在配置阶段将状态添加到 $ stateProvider
。如果要在运行时添加状态,则需要保持对 $ stateProvider
的引用。
See -edit- for updated information
Normally states are added to the $stateProvider
during the config phase. If you want to add states at runtime, you'll need to keep a reference to the $stateProvider
around.
此代码未经测试,但应该按照您的意愿行事。它创建一个名为 runtimeStates
的服务。您可以将其注入运行时代码,然后添加状态。
This code is untested, but should do what you want. It creates a service called runtimeStates
. You can inject it into runtime code and then add states.
// config-time dependencies can be injected here at .provider() declaration
myapp.provider('runtimeStates', function runtimeStates($stateProvider) {
// runtime dependencies for the service can be injected here, at the provider.$get() function.
this.$get = function($q, $timeout, $state) { // for example
return {
addState: function(name, state) {
$stateProvider.state(name, state);
}
}
}
});
我在 Future States 的东西http://christopherthielen.github.io/ui-router-extras =noreferrer> UI-Router Extras ,负责处理一些角落案例,例如将网址映射到不同的状态还存在。 Future States还展示了如何延迟加载运行时状态的源代码。请查看源代码了解所涉及的内容。
I've implemented some stuff called Future States in UI-Router Extras that take care of some of the corner cases for you like mapping urls to states that don't exist yet. Future States also shows how you can lazy load the source code for runtime-states. Take a look at the source code to get a feel for what is involved.
在UI中-Router 1.0,状态可以在运行时使用 StateRegistry.register
和 StateRegistry.deregister
。
In UI-Router 1.0, states can be registered and deregistered at runtime using StateRegistry.register
and StateRegistry.deregister
.
要获取访问StateRegistry,将其注入 $ stateRegistry
,或者注入 $ uiRouter
a nd通过 UIRouter访问它.stateRegistry
。
To get access to the StateRegistry, inject it as $stateRegistry
, or inject $uiRouter
and access it via UIRouter.stateRegistry
.
UI-Router 1.0还包括处理延迟加载状态定义的开箱即用的未来状态,甚至按网址同步。
UI-Router 1.0 also includes Future States out of the box which handles lazy loading of state definitions, even synchronizing by URL.
这篇关于AngularJS - UI路由器 - 以编程方式添加状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!