Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add class to first item in ng-repeat with jquery

I am new to complex directives and am trying to add a class to the first item in an ng-repeat with jquery (unfortunately my project is using it) like so inside a directive's controller:

   var highlightFirst = function(){
      $('.pointer').find('.fa-angle-down-first').next().addClass('boldit');
      console.log('in here')
    }

    highlightFirst();

    //also tried this:
    angular.element(document).ready(function(){
      $('.pointer').find('.fa-angle-down-first').next().addClass('boldit');
      console.log('in here')
    });

I invoke the function right after. nothing happens.

when I run $('.pointer').find('.fa-angle-down-first').next().addClass('boldit'); in chrome dev tools, what I want to be bolded is in fact bolded...what am I missing to get this specific solution working? and what is the best angular way to do this?

like image 847
devdropper87 Avatar asked Feb 07 '16 03:02

devdropper87


1 Answers

You don't actually need jquery for it and should avoid using jquery as much as possible in angular applications, you should use ng-class and $first here's the working demo

<ul>
    <li ng-repeat="item in items" ng-class="{'fooClass': $first}">{{ item }}</li>
</ul>

The good thing about $first is when you sort the ng-repeat it would still work correctly.

like image 160
Louie Almeda Avatar answered Oct 23 '22 13:10

Louie Almeda