【发布时间】:2020-03-18 18:34:57
【问题描述】:
给定下面的组件
export class VideoPlayerComponent implements AfterViewInit {
@ViewChild('videoPlayer', { static: false })
videoPlayer: ElementRef;
@Input()
videoUrl: string;
@Input()
videoType: string;
/** Subject that emits when the component has been destroyed. */
@Output()
onPlayerEvent = new EventEmitter<VideoPlayerEvent>();
videoJsPlayer: videojs.Player;
showTimestamp: boolean = false;
timeStamp: string;
constructor() { }
ngAfterViewInit() {
if (this.videoUrl) {
const self = this;
this.videoJsPlayer = videojs(this.videoPlayer.nativeElement, {}, function () {
this.on('play', () => self.onPlayerEvent.emit('play'));
this.on('pause', () => self.onPlayerEvent.emit('pause'));
this.on('ended', () => self.onPlayerEvent.emit('ended'));
return hls;
});
const myButton = this.videoJsPlayer.controlBar.addChild("button");
const myButtonDom = myButton.el();
myButtonDom.innerHTML = "<i class=\"material-icons\">\n" +
"query_builder\n" +
"</i>";
// @ts-ignore
myButtonDom.onclick = function(){
console.log('click');
this.showTimestamp = !this.showTimestamp;
console.log(this.showTimestamp);
};
}
}
}
每当 onclick 事件发生时,我都会尝试切换“showTimestamp”变量。这似乎在 'function(){}' 的范围内更新,但这并没有在整个组件的范围内更新。
我该如何解决这个问题?
【问题讨论】: