質問

I've tried doing the following in my controller:

  $scope.$on("$routeChangeStart", function (event, next, current) {
       if (!confirm('are you sure ?')) {
         event.preventDefault();           
       }
  });

But it doesn't work. Is that not supposed to be the way to do it ?

役に立ちましたか?

解決 2

I ended up using ui-router, so the way I'm doing it is:

$scope.$on('$stateChangeStart', function (event) {
     if (!confirm('are you sure ?')) {
         event.preventDefault();
     }
});

and it works.

他のヒント

I would put a directive on your links, that should be confirmed before changing the route. I just prototyped it in JSFiddle, I did not test it. But I think, this should be the proper way.

(function (angular) {
  module = angular.module('confirm', []);
  ConfirmDirective = function () {
      return {
        restrict: 'A',
        link: function (scope, elm, attrs, ctrls) {
            angular.element(elm).bind('click', function (event) {
                alert("Sure?");
                event.preventDefault();
                return false; //or true, depends on you
            });
        }
    };
  };
  module.directive("confirm", ConfirmDirective);
}(angular));

http://jsfiddle.net/L6xBF/3/

Check and try it.

Regards

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top