3 回答

TA貢獻1744條經(jīng)驗 獲得超4個贊
請參閱-edit-以獲取最新信息。
通常,狀態(tài)會$stateProvider在配置階段添加到中。如果要在運行時添加狀態(tài),則需要保留對$stateProvider周圍的引用。
此代碼未經(jīng)測試,但應(yīng)執(zhí)行您想要的操作。它創(chuàng)建了一個名為的服務(wù)runtimeStates。您可以將其注入運行時代碼,然后添加狀態(tài)。
// 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);
}
}
}
});
我已經(jīng)在UI-Router Extras中實現(xiàn)了一些稱為Future States的東西,這些東西為您處理了一些特殊情況,例如將url映射到尚不存在的狀態(tài)。Future States還顯示了如何延遲加載運行時狀態(tài)的源代碼??匆幌略创a,以了解其中涉及的內(nèi)容。
-edit-適用于UI-Router 1.0+
在UI-Router 1.0中,可以使用StateRegistry.register和在運行時注冊和注銷狀態(tài)StateRegistry.deregister。
要訪問StateRegistry,請將其作為$stateRegistry注入,或$uiRouter通過注入和訪問UIRouter.stateRegistry。
UI-Router 1.0還提供了開箱即用的Future States,可以處理狀態(tài)定義的延遲加載,甚至可以通過URL進行同步。
添加回答
舉報