Skip to content

Instantly share code, notes, and snippets.

@carmichaelize
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carmichaelize/e03d714d0cc5b8ebac82 to your computer and use it in GitHub Desktop.
Save carmichaelize/e03d714d0cc5b8ebac82 to your computer and use it in GitHub Desktop.
Check $dirty/$prestine state of an angular model.
(function () {
'use strict';
function $dirtyCheck(){
var watchers = [],
dirty = false;
return {
add: function($scope, model){
if ($scope && model){
var watcher = $scope.$watch(model, function(isDirty) {
if (isDirty){
dirty = true;
}
});
watchers.push(watcher);
}
},
clear: function(){
//De-register watchers
angular.forEach(watchers, function(watcher){
watcher();
});
watchers = [];
dirty = false;
},
check: function(){
return dirty;
}
}
}
angular
.module('APP_NAME')
.service('$dirtyCheck', $dirtyCheck);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment