Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角度:视子音频元素为HTMLOADIOLEMENT?_Angular_Typescript_Viewchild - Fatal编程技术网

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;
}