angularjs: preventing route changes -
i've tried doing following in controller:
$scope.$on("$routechangestart", function (event, next, current) { if (!confirm('are sure ?')) { event.preventdefault(); } });
but doesn't work. not supposed way ?
i put directive on links, should confirmed before changing route. prototyped in jsfiddle, did not test it. think, should 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 }); } }; }; module.directive("confirm", confirmdirective); }(angular));
check , try it.
regards