How to run a more advanced javascript code in an angular project - Stack Overflow

I need to run a javascript code in my angular project, and following other questions on SO I could run

I need to run a javascript code in my angular project, and following other questions on SO I could run a hello world js file. However, there is no example for usage of more advanced js files like cases in which js needs html elements of the angular component.

Following is my simplified myGame.js file:

export function greeting(){
    alert("Greeting from javascript");
}

var canvas, context;
export function initializeScenes() {
    canvas = document.getElementById("canvas");
    if (canvas)
        context = canvas.getContext("2d");
    else
        alert("No canvas");

}

and my runjavascriptponent.ts:

import * as myGame from "../../../../assets/myGame.js"
.
.
.
export class RunJavascriptComponent {
    constructor() {
       myGame.greeting();
       myGame.initializeScenes()

    }    
}


and my runjavascriptponent.htm:

<div class="container">
    <canvas id="canvas" width="600" height="400"></canvas>
</div>

When I run ng serve, I see "Greeting from javascript", which means greeting() is called successfully, but then I get the alert "No canvas", which I think means canvas element of my angular component.html is not recognized!

Does anyone know how to solve the problem? Thank you in advance.

I need to run a javascript code in my angular project, and following other questions on SO I could run a hello world js file. However, there is no example for usage of more advanced js files like cases in which js needs html elements of the angular component.

Following is my simplified myGame.js file:

export function greeting(){
    alert("Greeting from javascript");
}

var canvas, context;
export function initializeScenes() {
    canvas = document.getElementById("canvas");
    if (canvas)
        context = canvas.getContext("2d");
    else
        alert("No canvas");

}

and my runjavascriptponent.ts:

import * as myGame from "../../../../assets/myGame.js"
.
.
.
export class RunJavascriptComponent {
    constructor() {
       myGame.greeting();
       myGame.initializeScenes()

    }    
}


and my runjavascriptponent.htm:

<div class="container">
    <canvas id="canvas" width="600" height="400"></canvas>
</div>

When I run ng serve, I see "Greeting from javascript", which means greeting() is called successfully, but then I get the alert "No canvas", which I think means canvas element of my angular component.html is not recognized!

Does anyone know how to solve the problem? Thank you in advance.

Share asked Mar 4 at 8:31 user6781user6781 3452 silver badges14 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 3

If you want to access the HTML contents using angular you can use ngAfterViewInit hook, which is called after the HTML is initialized.

import * as myGame from "../../../../assets/myGame.js"
.
.
.
export class RunJavascriptComponent {
    
    ngAfterViewInit() {
       myGame.greeting();
       myGame.initializeScenes();
    }    
}

If you are open to modifying myGame.js convert the querySelector with a viewChild which is the angular way of accessing HTML elements.

import * as myGame from "../../../../assets/myGame.js"
.
.
.
export class RunJavascriptComponent {
    canvas = viewChild('canvas');
    
    ngAfterViewInit() {
       myGame.greeting();
       if (this.canvas()?.nativeElement) {
         myGame.initializeScenes(this.canvas().nativeElement);
       }
    }    
}

Then modify the JS as below:

export function initializeScenes(canvas: any) {
    if (canvas) {
        context = canvas.getContext("2d");
    } else {
        alert("No canvas");
    }
}

We need a template reference variable to refer the element, that is used inside the viewChild:

<div class="container">
    <canvas #canvas id="canvas" width="600" height="400"></canvas>
</div>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745055128a4608617.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信