Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular: How to load external scripts across the application in all the components

Script tags:

<script src="https://www.test.com/dem0.js"></script>
<script src="https://www.test.com/dem01.js"></script>
<script src="https://www.test.com/dem02.js"></script>
<script src="https://www.test.com/dem03.js"></script>

Index.html:

<!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <title>Mobile</title>
  <base href="/">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta name="wsstitle" content="catalog">
  <meta name="wssmlc" content="/us/en/">

  <link rel="icon" type="image/png" href="favicon.ico" sizes="32x32">
  <link href="https://fonts.googleapis.com/css?family=Roboto:300,300i,400,400i,700,700i" rel="stylesheet">
</head>

<body>
  <app-root></app-root>
</body>

</html>

I've few external <script> tags and I want to make them available in whole app (should be able to load em whenever my view changed in the browser). If I include them in my index.html <head> tag it's only working for the first time when the app initially load but for my case I've to load them whenever the view changed. Including the scripts in the <body> tag will be a good practice ??

like image 602
Ramana Avatar asked Jan 22 '26 18:01

Ramana


1 Answers

Add scripts reference path in angular-cli.json under

     "scripts": [
       "../src/assets/scripts/asn1.min.js"
      ],

To use it in component .ts file

declare var libraryName: any;

like image 159
rama krishna-rk Avatar answered Jan 24 '26 17:01

rama krishna-rk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!