Skip to content

Instantly share code, notes, and snippets.

@justinwinslow
Last active June 29, 2016 22:25
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 justinwinslow/2c205f75f5e30f92c676e0bd73cf8c34 to your computer and use it in GitHub Desktop.
Save justinwinslow/2c205f75f5e30f92c676e0bd73cf8c34 to your computer and use it in GitHub Desktop.
ng-actually-checked directive - For when you want to subvert checkbox clicks
angular.module('Module', [])
.directive('ngActuallyChecked', function(){
return {
link: function(scope, el, attrs) {
// Let's immediately store the value of our expression
var actualValue = scope.$eval(attrs.ngActuallyChecked);
// Don't let clicks overwrite our actual value
el.on('click', function(event){
el.prop('checked', actualValue);
});
// Let's watch for the value to change
scope.$watch(function(){
return scope.$eval(attrs.ngActuallyChecked);
}, function(checked){
// Update the stored value
actualValue = checked;
// Set checked
el.prop('checked', checked);
});
}
};
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment