0
<div has-class="{ 'open': isOpen, 'closed' : isClosed}"></div>

Is there a way to set the isOpen variable to true on the current scope from the directive as soon as the class that class changes.

This is sort of what i am trying to do with no luck

app.directive('hasClass', [function () {
var definition = {
    scope : {
        hasClass : "="
    },
    restrict: 'A',
    link: function (scope, element) {
        scope.$watch(function () {
            return element[0].className;
        }, function () {
            var items = {};
            angular.forEach(scope.hasClass, function (v, k) {
                if (element.hasClass(k)) {
                    scope.hasClass[k] = true;
                } else {
                    scope.hasClass[k] = false;
                }
            });
        });
    }
};

return definition;
}]);

Any suggestions are welcome , thank you

  • Why do you want to rely on a change in the state of the view to have an effect in state of your controller? Usually you do it the other way around, your controller tells you what the state is and then angularjs will modify the view for you based on that state. So if you are doing things in the angular way, the fact that an element has a class will be either because it was specified in the html or it was added because of a state change in your controller. – JoseM Jun 19 '14 at 18:20
  • a Jquery Plugin changes the classes and I would like to use this in a directive wrapping that jquery plugin , so the directives controller can know when the classes has changed. its basically using a plugin and overriding some styles on it but i need to know when to override.I am however just considering to rewrite the whole plugin and make a directive out of it. – user3232182 Jun 20 '14 at 08:53
  • I had a feeling it was something like that. Have you taken a look at [How do I “think in AngularJS” if I have a jQuery background?](http://stackoverflow.com/questions/14994391/how-do-i-think-in-angularjs-if-i-have-a-jquery-background) – JoseM Jun 20 '14 at 12:54

0 Answers0