下面是到目前为止的代码
<!doctype html>
<html ng-app>
<head>
<script src="http://code.angularjs.org/1.1.2/angular.min.js"></script>
<script type="text/javascript">
function Ctrl($scope) {
var initial = {text: 'initial value'};
$scope.myModel = angular.copy(initial);
$scope.revert = function() {
$scope.myModel = angular.copy(initial);
$scope.myForm.$setPristine();
}
}
</script>
</head>
<body>
<form name="myForm" ng-controller="Ctrl">
myModel.text: <input name="input" ng-model="myModel.text">
<p>myModel.text = {{myModel.text}}</p>
<p>$pristine = {{myForm.$pristine}}</p>
<p>$dirty = {{myForm.$dirty}}</p>
<button ng-click="revert()">Set pristine</button>
</form>
</body>
</html>
如何提醒在browser close
或url redirect
的情况下,有一些未保存的数据,使用户可以决定是否继续?
Answer 1:
像这样的东西应该这样做:
<!doctype html>
<html ng-app="myApp">
<head>
<script src="http://code.angularjs.org/1.1.2/angular.min.js"></script>
<script type="text/javascript">
function Ctrl($scope) {
var initial = {text: 'initial value'};
$scope.myModel = angular.copy(initial);
$scope.revert = function() {
$scope.myModel = angular.copy(initial);
$scope.myForm.$setPristine();
}
}
angular.module("myApp", []).directive('confirmOnExit', function() {
return {
link: function($scope, elem, attrs) {
window.onbeforeunload = function(){
if ($scope.myForm.$dirty) {
return "The form is dirty, do you want to stay on the page?";
}
}
$scope.$on('$locationChangeStart', function(event, next, current) {
if ($scope.myForm.$dirty) {
if(!confirm("The form is dirty, do you want to stay on the page?")) {
event.preventDefault();
}
}
});
}
};
});
</script>
</head>
<body>
<form name="myForm" ng-controller="Ctrl" confirm-on-exit>
myModel.text: <input name="input" ng-model="myModel.text">
<p>myModel.text = {{myModel.text}}</p>
<p>$pristine = {{myForm.$pristine}}</p>
<p>$dirty = {{myForm.$dirty}}</p>
<button ng-click="revert()">Set pristine</button>
</form>
</body>
</html>
请注意,$ locationChangeStart侦听器未在本例中触发,因为AngularJS不会在这样一个简单的例子,处理任何路由,但它应该在实际的角度应用程序。
Answer 2:
我已经延长了@Anders回答清理听众(解除绑定一线明星)时,指令被破坏(如:路由变化时),并增加了一些语法糖来概括的使用。
confirmOnExit指令 :
/**
* @name confirmOnExit
*
* @description
* Prompts user while he navigating away from the current route (or, as long as this directive
* is not destroyed) if any unsaved form changes present.
*
* @element Attribute
* @scope
* @param confirmOnExit Scope function which will be called on window refresh/close or AngularS $route change to
* decide whether to display the prompt or not.
* @param confirmMessageWindow Custom message to display before browser refresh or closed.
* @param confirmMessageRoute Custom message to display before navigating to other route.
* @param confirmMessage Custom message to display when above specific message is not set.
*
* @example
* Usage:
* Example Controller: (using controllerAs syntax in this example)
*
* angular.module('AppModule', []).controller('pageCtrl', [function () {
* this.isDirty = function () {
* // do your logic and return 'true' to display the prompt, or 'false' otherwise.
* return true;
* };
* }]);
*
* Template:
*
* <div confirm-on-exit="pageCtrl.isDirty()"
* confirm-message-window="All your changes will be lost."
* confirm-message-route="All your changes will be lost. Are you sure you want to do this?">
*
* @see
* http://stackoverflow.com/a/28905954/340290
*
* @author Manikanta G
*/
ngxDirectivesModule.directive('confirmOnExit', function() {
return {
scope: {
confirmOnExit: '&',
confirmMessageWindow: '@',
confirmMessageRoute: '@',
confirmMessage: '@'
},
link: function($scope, elem, attrs) {
window.onbeforeunload = function(){
if ($scope.confirmOnExit()) {
return $scope.confirmMessageWindow || $scope.confirmMessage;
}
}
var $locationChangeStartUnbind = $scope.$on('$locationChangeStart', function(event, next, current) {
if ($scope.confirmOnExit()) {
if(! confirm($scope.confirmMessageRoute || $scope.confirmMessage)) {
event.preventDefault();
}
}
});
$scope.$on('$destroy', function() {
window.onbeforeunload = null;
$locationChangeStartUnbind();
});
}
};
});
使用方法: 实施例控制器 :(在该示例中使用controllerAs语法)
angular.module('AppModule', []).controller('pageCtrl', [function () {
this.isDirty = function () {
// do your logic and return 'true' to display the prompt, or 'false' otherwise.
return true;
};
}]);
模板 :
<div confirm-on-exit="pageCtrl.isDirty()"
confirm-message-window="All your changes will be lost."
confirm-message-route="All your changes will be lost. Are you sure you want to do this?">
Answer 3:
安德斯的回答工作正常,但是,谁使用角UI路由器的人,你应该使用'$stateChangeStart'
,而不是'$locationChangeStart'
。
Answer 4:
我修改了@Anders回答,这样的指令不包含硬编码形式的名称:
app.directive('confirmOnExit', function() {
return {
link: function($scope, elem, attrs, ctrl) {
window.onbeforeunload = function(){
if ($scope[attrs["name"]].$dirty) {
return "Your edits will be lost.";
}
}
}
};
});
下面是它的HTML代码:
<form name="myForm" confirm-on-exit>
Answer 5:
也许这将是有帮助的人。 https://github.com/umbrella-web/Angular-unsavedChanges
使用该服务,您可以收听范围内的任何对象未保存的更改(不仅是形式)
Answer 6:
要使用安德斯Ekdahl的出色答卷有角1.5成分,注入$scope
在组件控制器:
angular
.module('myModule')
.component('myComponent', {
controller: ['$routeParams', '$scope',
function MyController($routeParams, $scope) {
var self = this;
$scope.$on('$locationChangeStart', function (event, next, current) {
if (self.productEdit.$dirty && !confirm('There are unsaved changes. Would you like to close the form?')) {
event.preventDefault();
}
});
}
]
});
Answer 7:
该接受的答案是伟大的,但我有正确得到我的表单控制器上的把手,因为一些形式我用一贯的问题form
标记与name
属性,并在其他时间我用的是ng-form
指令。 此外,如果你对使用了打字稿风格功能this
或vm
类型的模式如<form name='$ctrl.myForm'...
我很惊讶,没有人提到这一点,但我的修复是利用该指令的要求财产,让角给我的形式控制器本身的参考。
我已经更新了接受的答案下面显示我的变化,注意要求财产和链接功能的附加参数。
angular.module("myApp", []).directive('confirmOnExit', function() {
return {
restrict: 'A',
require: 'form',
link: function($scope, elem, attrs, form) {
window.onbeforeunload = function(){
if (form.$dirty) {
return "The form is dirty, do you want to stay on the page?";
}
}
$scope.$on('$locationChangeStart', function(event, next, current) {
if (form.$dirty) {
if(!confirm("The form is dirty, do you want to stay on the page?")) {
event.preventDefault();
}
}
});
}
};
});
有了这个,我可以保证,我有如下形式控制器上很好地处理,因为角将抛出一个错误,如果它不能找到元素上的表单控制器。
您还可以添加改性剂像^和? 如require='^form'
拉祖先形式或require='?form'
如果表单是可选的(这不会打破指令,但你需要检查你有一个手柄的有效形式控制器上自己) 。
文章来源: Detect unsaved changes and alert user using angularjs