Last active
June 29, 2016 22:25
-
-
Save justinwinslow/2c205f75f5e30f92c676e0bd73cf8c34 to your computer and use it in GitHub Desktop.
ng-actually-checked directive - For when you want to subvert checkbox clicks
This file contains 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
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