javascript - Angular ui 路由器 : warn user when leaving a specific page

标签 javascript angularjs angular-ui-router

在我的 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/

相关文章:

AngularJS:为桌面和移动使用不同的模板?

angularjs - 使用Ui-Router并遵循良好的设计模式

javascript - CSS/Javascript - 使用 css3 或 javascript 复制图像样式(贴纸样式)

javascript - $get ('0' 的含义)?

angularjs - Angular UI 日期选择器不更新

jquery - Angular-MVC 将 Angular Controller 移至单独的文件。现在如何加载@Model?

javascript - 使用相同功能的 Controller

javascript - 询问 ng-show/ng-hide 中的状态

javascript - 从对象面 Three.js 中检索纹理贴图坐标

javascript - jQuery - 如何选择具有相同类但不具有特定 ID 的所有元素