Last active
August 29, 2015 14:24
-
-
Save carmichaelize/e03d714d0cc5b8ebac82 to your computer and use it in GitHub Desktop.
Check $dirty/$prestine state of an angular model.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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