2

I've been learning Angular and now I'm trying to understand a large piece of code that was given me, and it has a lot of $() in the code containing a variable inside and a method call $(variable_name).method() or even a CSS class inside, between double quotes $(".class_name").method().

I understand $scope well, but I get confused with the syntax I explained above. Can someone explain what is it? Thanks in advance. /Teo

P.S.: The code is a directive, so I assume JS don't have this syntax except for the Angular framework.

teowey
  • 81
  • 6
  • 14
  • 2
    Possible duplicate of [What is the meaning of "$" sign in javascript](http://stackoverflow.com/questions/1150381/what-is-the-meaning-of-sign-in-javascript) – JJJ Apr 07 '16 at 10:26
  • 3
    That's just jQuery, it doesn't have anything to do with Angular. – Joe Clay Apr 07 '16 at 10:26

1 Answers1

4

Angular uses a subset of jQuery called jqLite. Here you can read a documentation: https://docs.angularjs.org/api/ng/function/angular.element Using $() function is basically wrapping an element so you can call jqLite function on it an chaining them. In your particular example $(variable_name).method() will wrap a DOM node stored in variable variable_name with jqLite and then run method on it. $(".class_name").method() is another usage of $(). It works the same as querySelectorAll() but instead of collection of DOM nodes will return collection of jqLite wraped nodes and then do the same - run method on each of them.

Bodzio
  • 2,440
  • 2
  • 19
  • 37
  • 1
    FYI: If you have jQuery included before angular then it will be a full jQuery object and not jQLite. – ste2425 Apr 07 '16 at 10:35
  • 2
    that's right :-) but you should always think twice before including jQuery in your angular project. Most of the things you can achieve with vanilla JS and angular itself, and using next library can slow down your website. – Bodzio Apr 07 '16 at 10:36
  • Thanks a lot @Bodzio! I was told this code I'm reading had only Angular on it. Now that you clarified that it contains JQuery and what it is about I can proceed look further on the latter. (: – teowey Apr 07 '16 at 10:50
  • It's not real jQuery it just looks like jQuery ;-) – Bodzio Apr 07 '16 at 10:51