While going away from page on click of any link, there could be a confirmation box on navigating away from a route.
We can handle (by confirmation) the cancelling of route change using $scope.$on function with event $routeChangeStart or $locationChangeStart and an Event handler function with :
- Event object
- Next route, where to navigate
- Current route, where to stay on cancel
app.controller("studentController", function ($route, $http, $scope) {
$scope.$on("$routeChangeStart", function (event, next, current) {
if(!confirm("Are you confirm to go away from this page ?")) {
event.preventDefault();
}
})
var curr = this;
curr.reloadData = function() {
$route.reload();
}
$http.get("service/api/GetAllStudents").then(
function(response) {
curr.students = response.data;
}
)
});
How to use the current route paths in confirmation box ?
next.$$route.originalPath WITH $routeChangeStart
Example
if(!confirm("Are you confirm to go away to page: " + next.$$route.originalPath)) {
event.preventDefault();
}
Output
Are you confirm to go away to page: /home
next WITH $locationChangeStart
Example
if(!confirm("Are you confirm to go away to page: " + next)) {
event.preventDefault();
}
Output
Are you confirm to go away to page: http://localhost:8080/proj/home
No comments:
Post a Comment
Note: only a member of this blog may post a comment.