javascript - 如何访问Angular 2中VideoJs函数中的成员变量和方法?

标签 javascript angular typescript video.js

我在我的 Angular 项目中使用 videojs 插件。我试图访问 videojs 方法中的值和方法,但在组件初始化时它显示未定义的值。我也尝试在 ngAfterViewInit 中调用 videojs 方法,但它仍然没有显示 videojs 方法中组件的值。如何在 videojs 方法中显示变量值?谁能帮我?

组件代码:

import {Component,OnInit} from '@angular/core';
declare var videojs :any;

@Component({
  selector: 'app-play-video',
  templateUrl: './play-video.component.html',
  styleUrls : [] 
 })
export class PlayVideoComponent implements OnInit{
public videoJSplayer :any;
public videoUrl :string;

constructor(){
this.videoUrl = 'http://clips.vorwaerts-gmbh.de/VfE_html5.mp4';
}
showValues(){
console.log("show Values method called"); 
}
  ngOnInit() {
    this.videoJSplayer = videojs(document.getElementById('play_video_id'), {}, function() { 
          console.log(this.videoUrl);  // here the video url value is undefined
          this.showValues(); // this also undefined
          this.play();
          }
    }
  }

播放视频.component.html:

<video id="play_video_id" class="video-js vjs-default-skin vjs-big-play-centered"
  controls preload="auto" width="640" height="264"
  poster="http://video-js.zencoder.com/oceans-clip.png"
  data-setup='{"example_option":true}'>
  <source [src] = videoUrl  type="video/mp4" />
</video>

最佳答案

你必须使用ES6 arrow functions以便回调在回调中获取正确的 this 上下文。当您使用 function() {} 语法时,内部的 this 将根据调用上下文而变化:

this.videoJSplayer = videojs(document.getElementById('play_video_id'), {}, () => { 
          // `this` will point to the current `PlayVideoComponent` instance
     }
)

关于javascript - 如何访问Angular 2中VideoJs函数中的成员变量和方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44254405/

相关文章:

javascript - 所有值均替换为 For 循环 TypeScript 中的最后一个值

javascript - 如何正确设计 JS 子伪类的原型(prototype)?

javascript - NgSelect 未在选择中选择选项

javascript - 处理由 from() 创建的 observable 中的 promise 抛出的错误

javascript - TypeScript 类中缺少方法

typescript - 在受约束的通用函数中混淆 "[ts] Type ... is not assignable to type [2322]"错误

javascript - 是否有一种方法只返回 ActionScript 中 RegEx 的匹配项或第一个变量匹配项或 null?

javascript - 对对象数组进行排序

javascript - 使用 Prototype 根据嵌套表中的类选择 td - javascript

javascript - 如何从一个 index.html 文件中的不同文件夹加载多个 angular2 组件?