I have a javascript file included in the <head></head> tags of my Angular 2 app. I would like to access a function in that javascript file from my typescript file, how should I do that? When I'm running the app, I can type var simplemde = new SimpleMDE() in the browser console to get my markdown editor working, but how can I do this in the constructor in my typescript file?
-
Start with some code. Show both your files (or part of them). So people can look at a specific example, not answer for something abstract.sphinks– sphinks2016-06-30 15:21:21 +00:00Commented Jun 30, 2016 at 15:21
Add a comment
|
2 Answers
Just declare it at the top of your component or wherever you need it like so:
declare var SimpleMDE: any;
Then you can call it in your constructor like you want!
7 Comments
Sander Shi
I tried this, but I'm getting the error:
TypeError: Cannot read property 'getAttribute' of undefined. I declared the variable above my component, and in my constructor I have let simplemde = new SimpleMDE();Maximilian Riegler
When doing
var simplemde = new SimpleMDE(); in the constructor?Sander Shi
Yes, I put that in the first line of my constructor.
Sander Shi
In my header tags I have
<script src="https://cdn.jsdelivr.net/simplemde/latest/simplemde.min.js"></script> and in the typescript file I have declare var SimpleMDE(): any; @Component({...}) export class MDEditor{constructor(){let simplemde = new SimpleMDE();}} @rinukkusuMaximilian Riegler
Please try declaring it without the paranthesis.
|
You can use namespacing, which is basically an object and it wraps all other variable & function
Like this
var SomeObject={}
SomeObject.abc = "Some Value";
SOmeObject.myFunction = function(){
// function body
}
In other file you can simply call SomeObject.abc & it will return string Some Value
You should use namespacing to avoid conflict of global window