Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Monitor for class changing on element in AngularJS directive

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!

like image 480
mikew Avatar asked Feb 11 '14 04:02

mikew


People also ask

What does $compile do in AngularJS?

Compiles an HTML string or DOM into a template and produces a template function, which can then be used to link scope and the template together.

What is Attrs in AngularJS?

Using attrs you are able to access the attributes defined in your html tag like <fm-rating ng-model="$parent.restaurant.price" symbol="$" readonly="true"> So in this case you will have access to the symbol and readonly attributes.

Which of the directive is used to display text in AngularJS?

ng-app: The ng-app Directive in AngularJS is used to define the root element of an AngularJS application. This directive automatically initializes the AngularJS application on page load. It can be used to load various modules in AngularJS Application.

Which directive do we use to inform AngularJS about the parts controlled by it?

The ngRef attribute tells AngularJS to assign the controller of a component (or a directive) to the given property in the current scope. It is also possible to add the jqlite-wrapped DOM element to the scope. The ngRepeat directive instantiates a template once per item from a collection.


1 Answers

works here

You may want to try this construction to follow class changes:

$scope.$watch(function() {return element.attr('class'); }, function(newValue){}); 

Please notice that adding/removing classes insight that watch will cause one additional call of adding class that wont change class attribute value.

But I think the better approach will be using ng-class. For example in a moment you want to add a 'popupOn' class you are setting some scope value ($scope.popup = true) and then specify ng-class for parent and child element like so:

<div ng-class={'has-error': (popup==true)}>     <div id="patient" ng-class={'popupOn': (popup==true)}>test</div>     <button ng-click="addClass()">addClass</button>     <button ng-click="removeClass()">removeClass</button> </div> 

when you want to remove those classes you just set $scope.popup value to false

Fiddle with ng-class

like image 136
Fedor Skrynnikov Avatar answered Sep 20 '22 18:09

Fedor Skrynnikov