在我的 Angular 应用程序中,我需要在用户离开 特定 页面之前警告用户,而不是每个页面。
如果我在要警告用户的页面 Controller 中使用 $locationChangeStart
,即使在进入页面时它也会触发,这是不希望的。
如果我在父 Controller 上使用它,它将在任何地方触发,我必须添加一个复杂的 if/else 或 switch 结构来基本上告诉它永远不会触发,除非用户放弃该特定状态。
如何使用 UI-Router 检测用户实际上离开(并且只离开)特定的状态
?
最佳答案
你应该使用一个事件(并钩住你自己的监听器)
$stateChangeStart
Fired when the state transition begins. You can use
event.preventDefault()
to prevent the transition from happening and then the transition promise will be rejected with a 'transition prevented' value....
Example:
$rootScope.$on('$stateChangeStart',
function(event, toState, toParams, fromState, fromParams){
event.preventDefault();
// transitionTo() promise will be rejected with
// a 'transition prevented' error
})
在这个问答中有详细的解释和工作plunker:
关于javascript - Angular ui 路由器 : warn user when leaving a specific page,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32588236/