我需要在使用Angular.js和Ui-router导航到另一个页面之前显示一条警报消息。我正在解释下面的代码。
<form name="billdata" id="billdata" confirm-on-exit enctype="multipart/form-data" novalidate>
<div id="SHOWDATA">
<div id="transactionsPortlet" class="panel-collapse collapse in">
<div class="portlet-body">
<div class="totalaligndiv">
<div class="col-md-6">
<div class="input-group bmargindiv1 col-md-12">
<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" >
</div>
<div class="input-group bmargindiv1 col-md-12">
<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>
<div class="input-group bmargindiv1 col-md-12">
<input type="button" class="btn btn-success" ng-click="addUserData(billdata);" id="addProfileData" ng-value="buttonName" />
</div>
<div class="clearfix"></div>
</div>
</div>
</div>
</div>
</form>
我的控制器页面代码如下所示。
var dashboard=angular.module('Channabasavashwara');
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.myForm.$dirty) {
if(!confirm("The form is not saved, do you want to stay on the page?")) {
event.preventDefault();
}
}
});
}
};
});
这里我的要求是假设用户填写了表单中的2个给定字段并突然导航到另一个页面而没有提交。在这种情况下,用户应该收到确认消息以留在此页面上。我在使用{{1导航视图。请帮我解决这个问题。