外部JavaScript不起作用的路由器变化

发布于 2025-01-30 12:08:14 字数 2292 浏览 2 评论 0原文

我正在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

素年丶 2025-02-06 12:08:14

l设法找到答案,以防万一有人面临同一问题。答案如下,您只需更改HTML和脚本即可。

我的.html文件

  <!-- put autoplay back -->
  <video playsinline controls onended="showUpNext()" class="video" id="video" *ngIf="video.video !== '/lessons/'">
    <source src="https://eworksa.co.za/Backend/{{ video.video }}" type="video/mp4" />
  </video>

 <!---Show these buttons if it is a video-->
      <div class="buttons" *ngIf="video.video !== '/lessons/'">
        <div class="mybuttons">
          <button type="button" class="btn btn-secondary" 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>

我的脚本。

   function showUpNext() {
  $("button").removeAttr("disabled");
}

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

  <!-- put autoplay back -->
  <video playsinline controls onended="showUpNext()" class="video" id="video" *ngIf="video.video !== '/lessons/'">
    <source src="https://eworksa.co.za/Backend/{{ video.video }}" type="video/mp4" />
  </video>

 <!---Show these buttons if it is a video-->
      <div class="buttons" *ngIf="video.video !== '/lessons/'">
        <div class="mybuttons">
          <button type="button" class="btn btn-secondary" 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

   function showUpNext() {
  $("button").removeAttr("disabled");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文