angularjs – 在导航到其他视图之前警告用户

前端之家收集整理的这篇文章主要介绍了angularjs – 在导航到其他视图之前警告用户前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
用户导航离开应用程序中的某些页面之前,我想警告用户并获得确认,例如撰写新的消息视图.我可以捕获哪些事件并取消/继续实现这一目标?

解决方法

如果您说某个链接或按钮导航到另一个路线或州,您只需显示一个确认导航的模式:

<a href="" ng-click="goToAnotherState()">Go Away...</a>

$scope.goToAnotherState = function(){

//show modal and get confirmating however your like
  if(modalResponse.Ok){
     $state.go('anotherState');
  }
}

另一个选择是在ui-router的$locationChangeStart事件中执行此操作,但如果您只在这里查看,那么第一种方法更好. $locationChangeStart方法

$rootScope.$on('$locationChangeStart',function (event,next,prev) {
  event.preventDefault();
  //show modal
  if(modalResponse.ok){
    var destination = next.substr(next.indexOf('#') + 1,next.length).trim();
    $location.path(destination)
  }
}

猜你在找的Angularjs相关文章