Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to redirect in angularjs

This might be a trivial thing to do, but i'm a angularjs newbie. Here is my angularjs controller code

    function MyCtrl1($scope, $location, $rootScope) {
  $scope.$on('$locationChangeStart', function (event, next, current) {
    event.preventDefault();
    var answer = confirm("Are you sure you want to leave this page?");
    if (answer) {

    }
  });
}
MyCtrl1.$inject = ['$scope', '$location', '$rootScope'];

In the next variable i have the url to redirect on confirm OK.But how to accomplish this thing in angularjs.

like image 328
iJade Avatar asked May 16 '26 21:05

iJade


1 Answers

You don't have to manually do it. Only cancel the event if they don't confirm:

$scope.$on('$locationChangeStart', function (event, next, current) {
    if ( ! confirm("Are you sure you want to leave this page?") ) {
        event.preventDefault();
    }
});
like image 192
Joseph Silber Avatar answered May 19 '26 09:05

Joseph Silber



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!