Last active
August 29, 2015 14:06
-
-
Save guilbep/136e34117f9016c37872 to your computer and use it in GitHub Desktop.
Directive to disable all childrens but some
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
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