1

I am new to Angular. I have added JS files in angular-cli.json inside scripts tag. Files are loaded successfully but the problem is that on my LoginForm console showing:

zone.js:192 Uncaught ReferenceError: theWYSIWYG is not defined

it is loaded when I loggedIn successfully I route to my another component from login-form.component.ts this.router.navigate(['dashboard']); on dashboard component some of html code is

<div id="theWYSIWYG" name="theWYSIWYG" frameborder="0" contentEditable="true"  dir="rtl" ></div>

function defined inside the js file which is loaded when app was started. Why JS is not accessing dashboard components.?

4
  • in tsconfig.json, do you have "allowJs": true under compilerOptions. This will allow typescript compiler to resolve the references to .js files. Commented Apr 21, 2018 at 10:21
  • becasuse your .js file execute before your component initilize. so before accessing element with id theWYSIWYG add condition for undefinded. Commented Apr 21, 2018 at 13:06
  • Yes! It is true. Commented Apr 21, 2018 at 19:59
  • When app starts all js are loaded first and it is through an console error that id=xyz not defined. while it is already defined in second component's html file. Commented Apr 21, 2018 at 20:11

2 Answers 2

1

if you want to use javascript in your component. you must be included before @Component

    declare var require: any;
    const myFunc = require('../assets/jsFile');
    @Component({ ....

and then inside your AppComponent you can call javascript function with myFunc .

ngOnInit() {
  myFunc.func1();
}
Sign up to request clarification or add additional context in comments.

3 Comments

I do not want to use these functions in component. I have js files in which all functions are already defined. But problem is that all JS are loads when app starts. At this time it does not see html file which is in another component (at this moment JS through error id=xyz is undefined while it is defined in 2nd component html file). but WHY?
What do you mean by this text ?" it is defined in 2nd component HTML file"
I have created 2components 1st named "LoginForm" and 2nd name "Dashboard" in this component I have dashboard.component.html. In which all these ids are.
0

import * as _ from "<jsfile_name>"; you can import js file in your component type script file

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.