下面是目前为止的代码
<!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>
Run Code Online (Sandbox Code Playgroud)
如何提醒browser close
或url redirect
在有未保存的数据的情况下,以便用户可以决定是否继续?
希望任何angularjs大师都可以帮助我.这是我的angularjs代码
$scope.$on('$routeChangeStart', function(event, next, current) {
if ($scope.myForm.$dirty) {
if(!confirm("Unsaved, do u want to continue?")) {
event.preventDefault();
}
}
});
Run Code Online (Sandbox Code Playgroud)
当数据变脏时,它会在浏览器后退按钮中发出警告,但是当点击取消或确定时它仍然完成路线更改.这样的话event.preventDefault()
就不起作用了.任何人都可以指出可能出错的地方