解决方法
如果您说某个链接或按钮导航到另一个路线或州,您只需显示一个确认导航的模式:
<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) } }