admin管理员组文章数量:1302984
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();
});
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.
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 ?
thanks !
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();
});
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.
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 ?
thanks !
Share asked Aug 27, 2014 at 14:49 Simon WatiauSimon Watiau 6376 silver badges18 bronze badges1 Answer
Reset to default 8Your 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();
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();
});
本文标签: javascriptuirouter deferIntercept and state paramsStack Overflow
版权声明:本文标题:javascript - ui-router deferIntercept and state params - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741712148a2393905.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论