How does Ionic ask the user before returning to the previous page: The current data has been modified. Do you want to confirm to abandon the modification and return? If the user selects "No", cancel the return operation and remain on the current page?
Writing the code to pop up the inquiry box in the $ionicView.beforeLeave event seems not possible, because when the event occurs, it has already returned to the previous page. And I don’t know how to cancel the default operation of this event. Using event.preventDefault() is invalid.
$rootScope.$on("$ionicView.beforeLeave", function (event, view) {
$ionicPopup.confirm({
title: "确认放弃修改",
template: "数据已经修改,是否确认放弃修改并返回?"
}).then(function (res) {
res || event.preventDefault();
});
});
The above code can pop up the confirmation dialog box, but when it pops up, the page has already slid to the previous page and it pops up too late. And event.preventDefault() is invalid and cannot prevent the page from leaving.