外部JavaScript不起作用的路由器变化
我正在使用Angular中的资产/JS/Script.js文件中的外部JavaScript来启用默认情况下禁用的按钮。但是,它仅在到另一个组件的路由下的第一页上工作,然后回到手表页面上,JavaScript未加载。在我的代码下面检查下方。
我的.ts文件
ngOnInit() {
// bug fix due js only working on refresh
this.loadScript('assets/js/script.js');
this.token = localStorage.getItem('token');
this.users = this.Token.payload(this.token);
this.username = this.users.data.name;
this.email = this.users.data.email;
this.videoid = this.getVideoId();
// console.log(this.video);
// get video by id sent from on click
this.jarwis.watch({ 'video_id': this.videoid }).subscribe((res: any) => {
this.video = res['video'];
});
}
// load js file in components
loadScript(url: string) {
const body = <HTMLDivElement>document.body;
const script = document.createElement('script');
script.innerHTML = '';
script.src = url;
script.async = false;
script.defer = true;
body.appendChild(script);
}
watch.html代码
<video controls class="video" id="video" *ngIf="video.video !== '/lessons/'">
<source src="https://testing.co.za/Backend/{{ video.video }}" type="video/mp4" />
</video>
<div class="buttons" *ngIf="video.video !== '/lessons/'">
<div class="mybuttons">
<button type="button" class="btn btn-secondary next" id="quizBtn" (click)="quiz(video.name)" disabled>
<span class="ti-agenda"></span>
QUIZ
</button>
</div>
<div class="divider"></div>
<div class="mybuttons">
<button type="button" class="btn btn-secondary next" id="bookBtn" (click)="book(video.subtopic)" disabled>
<span class="ti-calendar"></span>
Book Tutor
</button>
</div>
</div>
我的脚本。
window.onload = function () {
el = document.getElementById("video");
if (el) {
el.addEventListener("ended", videoEndHandler, false);
}
};
function videoEndHandler(e) {
document.getElementById("quizBtn").disabled = false;
document.getElementById("bookBtn").disabled = false;
}
我的
I'm using external javascript from my assets/js/script.js file in angular to enable buttons that are disabled by default. But it only works on the first page once l route to another component and comes back to the watch page the javascript is not loaded. check below my code.
my .ts file
ngOnInit() {
// bug fix due js only working on refresh
this.loadScript('assets/js/script.js');
this.token = localStorage.getItem('token');
this.users = this.Token.payload(this.token);
this.username = this.users.data.name;
this.email = this.users.data.email;
this.videoid = this.getVideoId();
// console.log(this.video);
// get video by id sent from on click
this.jarwis.watch({ 'video_id': this.videoid }).subscribe((res: any) => {
this.video = res['video'];
});
}
// load js file in components
loadScript(url: string) {
const body = <HTMLDivElement>document.body;
const script = document.createElement('script');
script.innerHTML = '';
script.src = url;
script.async = false;
script.defer = true;
body.appendChild(script);
}
my watch.html code
<video controls class="video" id="video" *ngIf="video.video !== '/lessons/'">
<source src="https://testing.co.za/Backend/{{ video.video }}" type="video/mp4" />
</video>
<div class="buttons" *ngIf="video.video !== '/lessons/'">
<div class="mybuttons">
<button type="button" class="btn btn-secondary next" id="quizBtn" (click)="quiz(video.name)" disabled>
<span class="ti-agenda"></span>
QUIZ
</button>
</div>
<div class="divider"></div>
<div class="mybuttons">
<button type="button" class="btn btn-secondary next" id="bookBtn" (click)="book(video.subtopic)" disabled>
<span class="ti-calendar"></span>
Book Tutor
</button>
</div>
</div>
my script.js
window.onload = function () {
el = document.getElementById("video");
if (el) {
el.addEventListener("ended", videoEndHandler, false);
}
};
function videoEndHandler(e) {
document.getElementById("quizBtn").disabled = false;
document.getElementById("bookBtn").disabled = false;
}
How do l make this external js file work even on router change?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
l设法找到答案,以防万一有人面临同一问题。答案如下,您只需更改HTML和脚本即可。
我的.html文件
我的脚本。
l managed to find the answer just in case someone faces the same issue. the answer is below, you only change the HTML and script.
my .html file
my script.js