Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should we use jQuery with AngularJS? [closed]

Our website is currently using jQuery library and getting a traffic of about 1 million monthly. We want to include API centric approach, so decided to move to Javascript MVC and have chosen angularJS for it.

Now my question is, should I use jQuery on the top of Angular so that I need to rewrite minimal DOM manipulation code, or I should re-write everything in the Angular way? We are using jQuery plugins like plupload, jQuery UI. etc on the website. Please suggest the best way of migration (page load time also matters).

Already went through "Thinking in AngularJS" if I have a jQuery background? but not getting a clear answer

like image 847
Ankur Aggarwal Avatar asked May 02 '15 21:05

Ankur Aggarwal


People also ask

Can we use jQuery and AngularJS together?

You can use JQuery together with AngularJS. AngularJS also have a lightweight version JQLite which contains all the JQuery functionality the AngularJS project needs.

Is jQuery necessary AngularJS?

This is to minimize dependencies. Yet, if you load jQuery before angular, then angular will use jQuery. Most of the time, you do not need to use jQuery. Even so much that, for beginners, it is advised to leave out jQuery completely as there would be a tendency to use jQuery when there is an easy / angular way.

Does AngularJS depend on jQuery?

Does AngularJS use the jQuery library? Yes, AngularJS can use jQuery if it's present in your app when the application is being bootstrapped. If jQuery is not present in your script path, AngularJS falls back to its own implementation of the subset of jQuery that we call jQLite.

Why you should not use jQuery with Angular?

Selectors and events are usually solved by libraries like React and Angular, so you don't need jQuery to help with browser compability and API differences.


1 Answers

Good thing about AngularJS which you need consider before doing Migration.

  1. It provides two way binding by only storing variable in scope.
  2. Other thing we need write code as compare to JQuery.
  3. Implementation in modular way(by creating angular.module)
  4. Shift most of the code from Javascript to HTML, that looks code more cleaner.
  5. Singleton patterners are there to store a data & share it between multiple controller or services.
  6. It in built with smaller version of jQuery that is known as JQlite which has most of the basic functionality, but you want to use JQuery in AngularJS then it will be available easily just you need to add jQuery reference in it, after that JQLite functionality gets converted into JQuery.

You should not use jQuery on the top of the AngularJS, because AngularJS digest cycle wont run if we do any angular DOM manipulation or scope variable manipulation using JQuery.

As you migrate you jQuery component to AngularJS you need to follow below things

  1. You need first search angular-ui-bootstrap site because they had covered most of the UI component which has already converted to angular.
  2. I'm sure you will not found every plugin Angular version, at that you should wrap that plugin to directive. Which will give you controller over the DOM element where ever directive has placed.(Example here for Datepicker using directive)
  3. Don't try to bind event from outside angular context that will create a digest cycle, that will leads to affect in binding updation on UI.
  4. Ensure while making any ajax call that should be using $http rather than using $.ajax
  5. There are many places you can find in jquery code that would be replace by ng-class directive, like if you are doing only adding & removing class, or showing some element on basis of any condtion, so that sort of jquery code can be replace by use ng-class directive
  6. Look for the places where you are only removing a DOM or adding DOM that could be easily replaced by the the ng-if directive, or only show hide of element can be done by using either ng-show/ng-hide
  7. Also find such a part in UI in which you are creating same DOM using for loop that can be convert to angular native directive ng-repeat
  8. If you have any case where you wanted to show and hide multiple element, so that part of code would be implemented using ng-switch directive
like image 186
Pankaj Parkar Avatar answered Oct 17 '22 02:10

Pankaj Parkar