0

I have a jquery code that checks for the class .slide on pageload. I have .slide classes inside my templates that it doesn't find and I am trying to figure out a way to make them find it. (probably by making a directive?)

If anyone has any ideas, I'd greatly appreciate it.

The code is as follows:

var items = $('.slide');
var content = $('.content');

function open() {
    $(items).removeClass('close').addClass('open');
}

function close() {
    $(items).removeClass('open').addClass('close');
}

$('#navToggle').on(clickevent, function(event) {
    event.stopPropagation();
    event.preventDefault();
    if (content.hasClass('open')) {
        close();
    } else {
        open();
    }
});
content.click(function() {
    if (content.hasClass('open')) {
        close();
    }
});
bryan
  • 8,879
  • 18
  • 83
  • 166

1 Answers1

0

You can create a directive with the same name as your class name:

Say you have two spans with one you need to distinguish using class 'myclass':

<div ng-app="ClassExample" ng-controller="someController">
    <span class="myclass myotherclass">target span</span><br/>
    <span class="myotherclass">other span</span>
</div>

Then the accompanying js code would include a directive definition using the name of the class 'myclass' and you can restrict this directive to classes using restrict:'C' :

angular.module('ClassExample',[]).directive('myclass', function() {
    return {
        template:'hello world',
        restrict: 'C'
    };

}).controller('someController',function() {

});

Here is the fiddle: https://jsfiddle.net/1L3h8y7s/1/

Ozan Tabak
  • 662
  • 3
  • 9