3

I'm using an angular directive and I am not having any luck with the jQlite .find() method:

DIRECTIVE

function cardsList () {
    return {
        restrict: 'A',
        controller: 'CardsController',
        templateUrl: 'app/directives/cards-list/cards-list.html',
        link: function ($scope, $element, attr, CardsController) {
                var cardLink = $element.find('a');

                console.log(cardLink);
            });

        }
    }
}

contextCards.directive('cardsList', cardsList);

An empty [] gets logged on the console.

TEMPLATE

<li data-ng-repeat="card in cards" class="cards--item">
    <a class="cards--link" data-ng-href="#/{{ card.slug }}">{{ card.title }}</a>
</li>

VIEW

<ul class="col-xs-12 cards--list" cards-list></ul>

All I want to do is traverse to the <a> elements. According to the docs, .find() only works on tag names which is exactly what I'm trying to do.

EDIT: I want to add a class to the <a></a> if the card the link represents is selected (like .current-card)

  • You're both right `ng-repeat` is the problem. I want to add a class to the `` if the card the link represents is selected (like `.current-card`). So I put the `
  • ` in the view and just have `` in the template. Is it good practice to `ng-repeat` a directive?
  • – Diego Hernandez Aug 08 '15 at 22:15
  • @DiegoHernandez, you should edit the question to add your ultimate goal. The solution could be simple with `ng-class` directive. Yes, it *is* a good practice the `ng-repeat` - that's what it is there for – New Dev Aug 08 '15 at 22:19