Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular throws errors whenever JQuery is included


NOTE

This is a link to a sample application that reproduces the issue


I am using SystemJS, JSPM, Angular and jQuery in an ASP.Net 4.6 application. However, whenever I try to include jQuery in the app, I get module load errors.

I uploaded a simple app to reproduce the issue to dropbox here: Simple App That Reproduces Issue

(you will need Visual Studio 2015 to run)

Essentially, in the project there is a file /scripts/app/app.js that looks like this:

import 'jquery';
import "angular";

var ByobApp = angular.module("ByobApp", []);

When you try and run the app when the code looks like this, you will see these errors (Chrome screenshot):

Module Load Errors

However, when you change the code to this:

//import 'jquery';
import "angular";

var ByobApp = angular.module("ByobApp", []);

(commenting out jQuery import). The application will load just fine.

Obviously something is wrong with the jQuery import. But I cannot tell what!

Any help would be great.


EDIT

Based on comments, I changed _Layout.cshtml to look like this (including JQuery and Angular without trying to load it with SystemJs):

<script src="~/scripts/jspm_packages/npm/[email protected]/dist/jquery.min.js"></script>
<script src="~/scripts/jspm_packages/github/angular/[email protected]/angular.min.js"></script>
<script src="/scripts/jspm_packages/system.js"></script>
<script src="/scripts/config.js"></script>
<script>
    System.baseURL = "/scripts";
</script>

And I made app.js look like this:

//import 'jquery';
//import "angular";

var ByobApp = angular.module("ByobApp", []);

The error is the same.


EDIT 2

If I include the Zepto library instead, it works fine. Zepto is a 1:1 replacement for the jQuery API, so using it is exactly the same!

like image 841
CleverPatrick Avatar asked Jan 20 '16 15:01

CleverPatrick


People also ask

Why jQuery is not used in Angular?

It adds a lot to bundle size which is very bad for slow networks and CPUs (mobile!). 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.

Can you use jQuery with Angular?

jQuery is a small yet feature-rich powerful javascript library that helps to manipulate the HTML DOM with less javascript code. We can use jQuery with Angular. There are some situations you come across while building Angular apps that it's absolutely necessary to use a library like jQuery to get something done.

Is not defined error in jQuery?

You may experience the “jQuery is not defined error” when jQuery is included but not loaded. Make sure that it's loaded by finding the script source and pasting the URL in a new browser or tab. The snippet of text you should look for to find the URL to test.

What is jQuery vs Angular?

What is the difference between Angular and jQuery? jQuery is a feature-rich Javascript library that is primarily used for DOM manipulation whereas Angular is a front-end development framework that is used for creating single-page applications. Angular employs two-way data binding to adapt and extend HTML codes.


1 Answers

This behaviour is (unfortunately) to be expected when using modules to load Angular. From the documentation on initialization:

Angular initializes automatically upon DOMContentLoaded event or when the angular.js script is evaluated if at that time document.readyState is set to 'complete'.

In the modules case, Angular is loaded when document.readyState is 'complete'.

At this point Angular looks for the ng-app directive which designates your application root. If the ng-app directive is found then Angular will:

  • load the module associated with the directive
  • ...

Remember, Angular is loaded as a dependency of app.js, so it executed before app.js, i.e. the ByobApp does not exist! So the first step above fails and you get the error.

You must use manual initialization (as suggested in the comment from Etsitra) - the only way to use Angular with module loaders AFAIK. This works:

  • remove ng-app from your main HTML file
  • add the following to app.js:

    angular.bootstrap(document, ['ByobApp']);
    
like image 190
Nikos Paraskevopoulos Avatar answered Sep 27 '22 18:09

Nikos Paraskevopoulos