Angular 角度:视子音频元素为HTMLOADIOLEMENT?
我试图在组件中获取Angular 角度:视子音频元素为HTMLOADIOLEMENT?,angular,typescript,viewchild,Angular,Typescript,Viewchild,我试图在组件中获取音频元素。起初我是用老式的方式做的: $player: HTMLAudioElement; ... ngOnInit() { this.$player = document.getElementById('stream') } 但我想用有角度的方式来做™所以我想我应该使用@ViewChild。但是它返回一个ElementRef,我必须反复钻研nativeElement属性才能对元素进行实际操作,而且它看起来更混乱 我想做这样的事情: @ViewChild('stream'
音频元素。起初我是用老式的方式做的:
$player: HTMLAudioElement;
...
ngOnInit() {
this.$player = document.getElementById('stream')
}
但我想用有角度的方式来做™所以我想我应该使用@ViewChild
。但是它返回一个ElementRef
,我必须反复钻研nativeElement
属性才能对元素进行实际操作,而且它看起来更混乱
我想做这样的事情:
@ViewChild('stream')$player:HTMLAudioElement代码>
但这不起作用。如果您将setter与ViewChild
关联,则可以初始化其中的HTMLAudioElement
属性,然后使用该属性:
$player: HTMLAudioElement;
@ViewChild('stream') set playerRef(ref: ElementRef<HTMLAudioElement>) {
this.$player = ref.nativeElement;
}
有关演示,请参阅。使用view child访问本机DOM元素时,它将始终包装在ElementRef中。
@ViewChild('stream') playerRef: ElementRef<HTMLAudioElement>;
get $player(): HTMLAudioElement {
return this.playerRef.nativeElement;
}