AngularJs UI-Router - 页面刷新 $state.current 现在是空的
问题说明
我有一个使用 ui-router
的 Angular
应用程序,每当我刷新页面时都会遇到问题.我正在使用嵌套视图、命名视图来构建应用程序.每当我刷新页面时,ui-router
都不会重新加载当前状态,而是将页面留空.
I have an Angular
application using ui-router
and I am having issues whenever I refresh the page. I am using nested views, named views to build the application. Whenever I refresh the page, ui-router
doesn't reload the current state and just leaves the page blank.
页面加载 $state.current
等于
Object {name: "", url: "^", views: null, abstract: true}
我正在通过 $http
从 .json
文件中读取我的导航并循环浏览状态.所以这就是我可以展示的:
I am reading my navigation from a .json
file via $http
and looping through the states. So this is what I can show:
stateProvider.state(navElement.stateName, {
url: navElement.regexUrl ? navElement.regexUrl : url,
searchPage: navElement.searchPage, //something custom i added
parent: navElement.parent ? navElement.parent : "",
redirectTo: navElement.redirectTo,
views: {
'subNav@index': {
templateUrl: defaults.secondaryNavigation,
controller: 'secondaryNavigationController as ctrl' //static
},
'pageContent@index': {
template: navElement.templateUrl == null
? '<div class="emptyContent"></div>'
: undefined,
templateUrl: navElement.templateUrl == null
? undefined
: navElement.templateUrl,
controller: navElement.controller == null
? undefined
: navElement.controller ' as ctrl'
}
}
});
为嵌套的 json 对象中的每个项目执行此代码.如果还有其他有用的信息,请告诉我.
This code gets executed for each item in the nested json object. If there is anything else that would be helpful, let me know.
正确答案
有个问题:AngularJS - UI-router - How to使用一个答案配置动态视图,这说明了如何做到这一点.
There is a question: AngularJS - UI-router - How to configure dynamic views with one answer, which shows how to do that.
发生了什么?刷新时,url
会更快地进行评估,然后注册状态.我们必须推迟它.解决方案由 UI-Router
原生特性 deferIntercept(defer)
What is happening? On refresh, the
url
is evaluated sooner, then states are registered. We have to postpone that. And solution is driven byUI-Router
native featuredeferIntercept(defer)
$urlRouterProvider.deferIntercept(推迟)
如文档中所述:
$urlRouterProvider.deferIntercept(defer)
As stated in the doc:
禁用(或启用)延迟位置更改拦截.
Disables (or enables) deferring location change interception.
如果您希望自定义同步 URL 的行为(例如,如果您希望延迟转换但保留当前 URL),请在配置时调用此方法.然后,在运行时,在您配置自己的 $locationChangeSuccess
事件处理程序后调用 $urlRouter.listen()
.
If you wish to customize the behavior of syncing the URL (for example, if you wish to defer a transition but maintain the current URL), call this method at configuration time. Then, at run time, call $urlRouter.listen()
after you have configured your own $locationChangeSuccess
event handler.
简而言之,我们将在配置阶段停止 URL 处理:
In a nutshell, we will stop URL handling in config phase:
app.config(function ($urlRouterProvider) {
// Prevent $urlRouter from automatically intercepting URL changes;
// this allows you to configure custom behavior in between
// location changes and route synchronization:
$urlRouterProvider.deferIntercept();
})
一旦我们从 JSON 配置了所有动态状态,我们将在 .run() 阶段重新启用它:
And we will re-enable that in .run() phase, once we configured all dynamic states from JSON:
.run(function ($rootScope, $urlRouter, UserService) {
...
// Once the user has logged in, sync the current URL
// to the router:
$urlRouter.sync();
// Configures $urlRouter's listener *after* your custom listener
$urlRouter.listen();
});
有来自链接的 问答一个
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tanhcgkgkc
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
复制文件夹/文件而不修改属性?
it1352 07-15