When a class is added to an element, I want to add another class to that element. When a class is removed, I want to remove an element. I'm basically mapping some bootstrap classes to some angular form validation classes but I can't figure out how to fire my code when a class is added/removed from an element (also making sure not to cause some infinite loop of class changing).
Here is what I have thus far for my directive:
.directive('mirrorValidationStates', ['$log', function($log) {
function link(scope,element,attrs) {
scope.$watch(element.hasClass('someClass'), function(val) {
var classMap = {
'popupOn': 'has-error',
'ng-valid': 'has-success'
};
for (var key in classMap) {
if (element.hasClass(key)) {
$log.info("setting class " + classMap[key]);
element.parent().addClass(classMap[key]);
} else {
$log.info("removing class " + classMap[key]);
element.parent().removeClass(classMap[key]);
}
}
});
}
return {
link: link
};
}]);
So basically, when popupOn
is added to an element, I want to add the has-error
bootstrap class to the parent element. When popupOn
is removed, I want to remove has-error
.
What am I missing to make this happen in Angular? Maybe something with ng-class and not use a directive?
Thanks much!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…