Skip to content

Instantly share code, notes, and snippets.

@guilbep
Last active August 29, 2015 14:06
Show Gist options
  • Save guilbep/136e34117f9016c37872 to your computer and use it in GitHub Desktop.
Save guilbep/136e34117f9016c37872 to your computer and use it in GitHub Desktop.
Directive to disable all childrens but some
module.directive('xngDisabled', function($parse) {
return {
restrict: 'A',
scope: {
xngDisabled : "@"
},
link: function (scope, element, attrs, ctrl) {
var setDisable = function (el, disable){
if(disable === "true") {
//condition-disabled
el.addClass('disabled');
el.attr('disabled', true);
} else {
// remove class
el.removeClass('disabled');
el.removeAttr('disabled');
}
// apply to all children input! but the one with
// // do-not-disable attr
angular.forEach(el.children(), function(elChild){
var angularChildEl = angular.element(elChild);
if (!angularChildEl.attr('do-not-disable')){
setDisable(angularChildEl, disable);
}
});
};
scope.$watch('xngDisabled', function(newv, oldv) {
setDisable(element, newv);
});
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment