UI路由器deferIntercept和国家PARAMS [英] ui-router deferIntercept and state params
问题描述
我使用的UI路由器的新deferIntercept()无需重新加载更新浏览器URL
我的控制器:
I use the new deferIntercept() of ui-router to update the browser url without reloading my controller:
$rootScope.$on('$locationChangeSuccess', function(e, newUrl, oldUrl) {
e.preventDefault();
if ($state.current.name !== 'search') {
$urlRouter.sync();
}
$urlRouter.listen();
});
有了这个code,在浏览器的后退按钮点击更改网址为previous之一,但我不能更新我的控制器状态,以反映这种变化。
$ stateParams仍然包含值设置当用户第一次加载页面。
With this code, a click on the browser's back button changes the URL to the previous one, but I'm not able to update my controller state to mirror this change. $stateParams still contains the values set when the user first loaded the page.
什么是更新$状态的最佳方式和$ stateParams我的控制器内的对象,当用户单击后退按钮或手动更改URL?
What's the best way to update the $state and $stateParams objects inside my controller when the user click the back button or change the URL manually ?
谢谢!
推荐答案
您调用 $ urlRouter.listen()
应该放在事件处理程序之外。您提供的code段应改为:
Your call to $urlRouter.listen()
should be placed outside the event handler. The code snippet you provided should be changed to:
$rootScope.$on('$locationChangeSuccess', function(e, newUrl, oldUrl) {
e.preventDefault();
if ($state.current.name !== 'search') {
$urlRouter.sync();
}
});
// Moved out of listener function
$urlRouter.listen();
来源:的的 $ urlRouter =nofollow的>官方文档提供了一个code样本 deferIntercept
方法。它调用放置到 $ urlRouter.listen()
监听功能外:
Source: The official documentation for $urlRouter
provides a code sample for the deferIntercept
method. It places the call to $urlRouter.listen()
outside of the listener function:
var app = angular.module('app', ['ui.router.router']);
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();
}).run(function ($rootScope, $urlRouter, UserService) {
$rootScope.$on('$locationChangeSuccess', function(e) {
// UserService is an example service for managing user state
if (UserService.isLoggedIn()) return;
// Prevent $urlRouter's default handler from firing
e.preventDefault();
UserService.handleLogin().then(function() {
// 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();
});
这篇关于UI路由器deferIntercept和国家PARAMS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!