Okay, after reading your bounty comment, I understand that you want this to be controller independent. I spent some time completely reworking my solution, and I think I have finally figured out a way to accomplish what you want.
It really comes down to 2 things:
1) Detecting a change on the checkbox :checked status, and
2) Detecting a change on the checkbox :disabled status
Detecting 1) was easy, as you can use a simple jQuery change handler, but detecting 2) took a bit more research. It requires the use of scope.$watch on the child ng-disabled attribute.
Here is a demo of how this would work:
var app = angular.module("myApp", [])
.directive("classWhen", function() {
function setClasses(classWhen, $element, $input) {
Object.keys(classWhen).forEach(function(key) {
var test = classWhen[key];
// .toggleClass("className", true) === .addClass("className")
// .toggleClass("className", false) === .removeClass("className")
$element.toggleClass(key, $element.is(test));
});
}
return {
restrict: 'A',
link: function link (scope, element, attrs) {
var classWhen = JSON.parse(attrs.classWhen);
var $element = $(element);
$element.find("*").each(function (index, elem) {
var $elem = $(this);
// namespace the change event so we can easily .off() it
$elem.off("change.classWhen");
$elem.on("change.classWhen", function () {
setClasses(classWhen, $element, $elem);
});
// watch child ng-disabled attribute
scope.$watch($elem.attr("ng-disabled"), function (val) {
setClasses(classWhen, $element, $elem);
});
});
}
};
});
.is-checked {
background-color: yellow;
}
.is-disabled {
background-color: lightblue;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div ng-app="myApp">
<label>
<input type="checkbox" ng-model="disableAll" />Disable All</label>
<br>
<br>
<label class-when='{"is-checked": ":has(input:checked)", "is-disabled": ":has(input:disabled)"}'>
<input type="checkbox" ng-disabled="disableAll">Example checkbox 1
</label>
<br>
<label class-when='{"is-checked": ":has(input:checked)", "is-disabled": ":has(input:disabled)"}'>
<input type="checkbox" ng-disabled="disableAll">Example checkbox 2
</label>
<br>
<label class-when='{"is-checked": ":has(input:checked, .test)", "is-disabled": ":has(input:disabled)"}'>
<input type="text" ng-disabled="disableAll" ng-class="testingClass" ng-model="testingClass"/>
<br>
<input type="checkbox" ng-disabled="disableAll">
Example checkbox 3
</label>
<br>
<label class-when='{"is-checked": ":has(input:checked)", "is-disabled": ":has(input:disabled)"}'>
<input type="checkbox" ng-disabled="disableAll">Example checkbox 4
</label>
</div>
scope.objectis a string, so doing_.forOwn()gives you each character in the string with an index as the key. To fix this, you need to do aJSON.parse()onscope.object, which leads to the next issue. Your string is invalid JSON because it has single quotes surrounding the properties/values and it needs double quotes to be valid JSON. The issue then remains that your directive does not fire when the user checks/unchecks the checkbox. I'm still working on that part, but I think i've given you a big step in the right direction"="binding for the scope.object.