JSFiddle - React, Tailwind, and code Playground
HTML
<script src="http://code.angularjs.org/angular-1.0.1.js"></script>
<div ng-app="myApp">
<form saving-form="" name="myform">
<input type="text" name="odin" ng-model="odin" required=""/><br/>
<input type="text" name="dva" required=""/><br/>
<input type="text" name="tri"/><br/>
dirty: {{myform.$dirty}}<br/>
valid: {{myform.$valid}}<br/>
</form>
</div>
JavaScript
var myApp = angular.module('myApp',[]);
myApp.directive(
'savingForm',
function() {
return function($scope, el, attrs) {
window.onbeforeunload = function() {
if($scope[el.attr('name')].$dirty && $scope[el.attr('name')].$valid) {
return 'Не сохранённые данные будут потеряны.';
}
};
}
}
);