I have one issue here.I have a form in my app and my requirement is while user is navigating to another page without submit this form one confirmation message will display to alert user using angular.js.I am explaining my code below.
<form name="billdata" id="billdata" confirm-on-exit enctype="multipart/form-data" novalidate>
<span class="input-group-addon ndrftextwidth text-right" style="width: 180px;">First Name :</span>
<input type="text" name="itemname" id="contactno" class="form-control" placeholder="user Name" ng-model="first_name">
<span class="input-group-addon ndrftextwidth text-right" style="width: 180px;">Last Name :</span>
<input type="text" name="itemname" id="contactno" class="form-control" placeholder="user Name" ng-model="last_name">
<div style="text-align: center; padding-top: 10px;">
<input type="button" class="btn btn-success" ng-click="addUserData(billdata);" id="addProfileData" value="submit" />
</div>
</form>
And i have the following code in my controller page.
dashboard.directive('confirmOnExit', function() {
return {
link: function($scope, elem, attrs) {
window.onbeforeunload = function(){
if ($scope.billdata.$dirty) {
return "The form is not saved, do you want to stay on the page?";
}
}
$scope.$on('$locationChangeStart', function(event, next, current) {
if ($scope.billdata.$dirty) {
console.log('next',next,current);
if(!confirm("The form is not saved, do you want to stay on the page?")) {
event.preventDefault();
}else{
console.log('hii');
}
}
});
}
};
});
Here i am getting the confirmation message while user is totally leaving the page site.But my requirement is while user is navigating to another page without submit this form the confirmation message should display.Please help me.