Is it a good idea to use jquery in angularjs directive to interact with dom elements?Thanks.
-
It's okay, why not. jQuery exists for DOM manipulations. Whether it's really needed or not - another question. Modern environments have pretty good support for convenient methods like querySelector(All), etc. so jQuery might not be really necessary.dfsq– dfsq2015-08-20 09:38:40 +00:00Commented Aug 20, 2015 at 9:38
-
Is it a good idea, some might say yes, others say no. It also depends on the situation I guess, but as @dfsq mentions, it's not necessary. AngularJS is powerfull enough to handle most of the things you can do in jQuery.cverb– cverb2015-08-20 10:08:10 +00:00Commented Aug 20, 2015 at 10:08
2 Answers
Sine the element is easily accessible in directive, you can do any DOM related operation in directive without using jQuery.
directive('yourDir', function() {
return {
restrict: 'E/A',
link: function(scope, element, attrs) {
//do your things here
}
}
}
Hence overflowing code with jQuery is not a good idea in my opinion. You can do all similar operations using angular. Adding an extra library for same operations is not good. Sometimes this also leads in making application heavy since the use of JQuery will be limited to DOM manipulation. Try creating simple operation using JavaScript only
Comments
It is okay to use JQuery in AngularJS. In fact, AngularJS allows the use by using angular.element instead of $(element) in JQuery.
You can read AngularJS Element.
However, in my experience, I was using the $(element) since it is convenient for me and I have more knowledge on JQuery accessing DOM compared to angularJS.
2 Comments
angular.element is going to be jQuery if it was loaded before Angular.