I have an angular 4 application and I have a javascript component that I created in a javascript file : timeline.js
. The javascript component works well but I want to use it with angular 4. So, I put my js file in the folder assets/js/timeline.js
.
In the file index.html
, I call my js file with <script src="assets/js/timeline.js"></script>
and in app.component.ts
, I have :
var timeline = new Timeline("timelineVisualization")
So, normally, the timeline is created in the div which has id="timelineVisualization"
.
But it doesn't work and I have an error on the new Timeline
: Cannot find name Timeline.
So, do you know how I can do to call the Timeline constructor from timeline.js
?
To include an external JavaScript file, we can use the script tag with the attribute src . You've already used the src attribute when using images. The value for the src attribute should be the path to your JavaScript file. This script tag should be included between the <head> tags in your HTML document.
The Angular 13 update adds RxJS, a reactive extension for JavaScript, and includes all versions of RxJS up to and including version 7. For apps created with ng new, RxJS 7.4 has become the default.
you simply need to do
import * as Timeline from '../assets/js/timeline.js';
You can also do (at the top of your file) :
declare var Timeline: any;
Check also below for good practices.
Just extending on the above answer by @Deblaton Jean-Philippe and as a general good practice, it might be better to include your js or other css files as part of the build instead of putting them in your index.html.
If you are using a bundler, use something like this.
"styles": [ "styles.scss", //Other style files ], "scripts": [ "../node_modules/jsplugin/dist/js/plugin.js", //Other script files ],
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With