angular-cli: Scripts not loading from angular.json file

Versions

Angular CLI: 6.0.3
Node: 8.9.4
OS: win32 x64
Angular:
...

Package                      Version
------------------------------------------------------
@angular-devkit/architect    0.6.3
@angular-devkit/core         0.6.3
@angular-devkit/schematics   0.6.3
@schematics/angular          0.6.3
@schematics/update           0.6.3
rxjs                         6.1.0
typescript                   2.7.2
Windows 10x64

Repro steps

I need to use admin-lte in my project

  • Step 1: I added it via terminal using npm install admin-lte --save
  • Step 2: I added jquery via terminal using following: npm install --save jquery npm install --save-dev @types/jquery
  • Step 3: I added all the necessary .css and .js files in the angular.json file including first script reference of jquery from nod_modules

“scripts”: [ “node_modules/jquery/dist/jquery.js”, “node_modules/admin-lte/plugins/bootstrap/js/bootstrap.min.js”, “node_modules/admin-lte/plugins/fastclick/fastclick.js”, “node_modules/admin-lte/dist/js/adminlte.min.js”, “node_modules/admin-lte/plugins/slimscroll/jquery.slimscroll.min.js”, “node_modules/admin-lte/plugins/select2/select2.full.min.js”, “node_modules/ion-rangeslider/js/ion.rangeSlider.min.js”, “node_modules/alertifyjs/build/alertify.min.js”, “node_modules/datatables.net/js/jquery.dataTables.js” ]

Observed behavior

jquery and other js files are not loading

Desired behavior

I have used a collapsable panel from the admin-lte template and it should toggle

Note that other javascript files like ion slider , alertify and jquery.dataTables are working.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 3
  • Comments: 33 (3 by maintainers)

Most upvoted comments

Try move this scripts to src/assets/ folder and change all url like “src/assets/node_modules/jquery/dist/jquery.js”

Solution (hidden from view)

Hello All: When you add a script or style to the angular.json file in angular@6+ (or in angular-cli.json in angular@5orEarlier), the expected results is that those js files whether they are bootstrap.min.css or jquery.min.js will get processed and combined into the new style.js or scripts.js files.

When you examine those files (style.js or scripts.js)… you will notice the derivatives of the files such as bootstrap.min.css or jquery.min.js will be in them.

angular-support_1

This is by design. If you examine the contents of either style.js or scripts.js and you fail to see the content block - then you need to investigate further.

Hope this helps clarify the issue. I believe this issue can be closed.

So with Angular we can’t load any external js file when we create a routing project because after the first routing change all the scripts are “destroyed” and not loaded again ?..

Angular is a framework for Single Page Applications (SPAs). When using a SPA, parts of your DOM and added and deleted at runtime, without a page reload.

@cactusjack66’s repro uses some bootstrap scripts:

            "scripts": [
              "node_modules/admin-lte/bower_components/jquery/dist/jquery.min.js",
              "node_modules/admin-lte/bower_components/bootstrap/dist/js/bootstrap.min.js",
              "node_modules/admin-lte/dist/js/adminlte.min.js"
            ]

The way these work is by looking up the DOM, finding elements with certain CSS or ids, and attaching behaviour to them. This happens only when on the initial load because that’s how script tags work in HTML.

Let’s think about the order of these things. First you load your app, then angular takes over and renders your initial components, and then bootstrap sees these components and attaches some behaviour to them. At this point bootstrap has finished running.

When you then go to another route, the original component is removed from the DOM and the new one added. Bootstrap doesn’t run again, because scripts run only once. The newly rendered component do not get the bootstrap behaviour.

This is all intended and is how scripts work. You could re-add the bootstrap bindings on every DOM change but that’s really slow and takes a lot of work. My best advice is to use something like https://ng-bootstrap.github.io/ instead, because it takes care of attaching that dynamic behavior for you.

if it is only working for the first time This answer worked for me. Check this out

https://stackoverflow.com/a/55675403/6314955

you have to load the script inside component.ts file where you want

@filipesilva the documentation seems to be incorrect.

You can add Javascript files to the global scope via the scripts option inside your project’s build target options in angular.json. These will be loaded exactly as if you had added them in a <script> tag inside index.html.

Currently it does not behave exactly like a script tag. For example, Adding the following:

            "scripts": [
              "node_modules/jquery/dist/jquery.slim.js",
              "node_modules/popper.js/dist/popper.js",
              "node_modules/bootstrap/dist/js/bootstrap.js"
            ]

Should allow bootstrap dropdowns to work, but they do not. However, if I add the <script> tags in index.html they will work.

use this syntax: “./node_modules/jquery/dist/jquery.js” instead of “node_modules/jquery/dist/jquery.js”

I’m not having the problem for build but for test. Initially karma gave me undefined, then I added the scripts, and that error went away. However my test code cannot access the global variables. I’ve tried using files option in karma config that doesn’t help either.