0

I have a simple directive that toggles a class on an element, is it better to use $(this) or 'element' inside the function? (both work fine)

Here's the example:

.directive('test',['$rootScope',function($rootScope){
        return {
            restrict:'C',
            link: function(scope, element, attrs){
                return element.bind('click',function(){
                    element.toggleClass("active");
                });
            }
        }
    }])
Jon Snow
  • 3,682
  • 4
  • 30
  • 51
  • 3
    I would go with element. Try to use as much angular as possible and a minimum of jQuery. If you get more experienced in angular you start to remove more and more jQuery from you source code. 90% of what you want to do you can do without the need for jQuery. Use descriptive views instead of dom manipulations using jQuery. – bekite Nov 03 '13 at 11:18
  • 1
    `link` gives you ability to use `element` defined as parameter. So use it. I think its right way – Maxim Shoustin Nov 03 '13 at 13:15
  • I will suggest same as Bekite. Go with element. – Amritpal singh Nov 04 '13 at 16:16

0 Answers0