【发布时间】:2016-02-19 15:33:28
【问题描述】:
我正在使用 Angular 2 (TypeScript)。我想重写下面的代码。
<video></video>
var video = document.querySelector("video");
video.src = URL.createObjectURL(localMediaStream);
video.play();
但是,我不知道在 Angular 2 中获取“视频”的任何方法。到目前为止,我就是这样做的。
<video [src]="videoSrc"></video>
videoSrc:string;
…
this.videoSrc = URL.createObjectURL(stream);
// Since I don't know any way to get "video",
// do I have any way to use video.play() here?
【问题讨论】:
-
迟到的评论。 . .我知道@ViewChild 的答案适用于这种情况,但如果有人真的需要做querySelector,我认为
constructor(private el: ElementRef) { const result = this.el.nativeElement.querySelector('.myClass'); }会比const result = document.querySelector('.myClass');更好,就像angular.io/guide/attribute-directives 一样,尽管angular.io/api/core/ElementRef 说“使用这个API作为最后的手段。”
标签: angular typescript