I am writing a component where I need access to the <audio controls>
native element. I am doing it like this now by getting it in ngOnInit()
by using ElementRef
like this this.elementRef.nativeElement.querySelector("audio");
While it works I think it is very unelegant and Angular2 also warns of the risks when using ElementRef..
Is there really no simpler way?
Can I mark it as a local variable with <audio controls #player>
and somehow access the native element through this.player
or something similar from the controller?
import {Component, OnInit, ElementRef, Input} from 'angular2/core'; @Component({ selector: 'audio-preview', template: ` <audio controls> <source [src]="src" type="audio/mpeg"> Your browser does not support the audio element. </audio> ` }) export class AudioPreview implements OnInit { @Input() src: string; constructor(public elementRef: ElementRef) {} ngOnInit() { var audioElement = this.getAudioElement(); audioElement.setAttribute('src', this.src); } getAudioElement() : HTMLAudioElement { return this.elementRef.nativeElement.querySelector("audio"); } }
To get started using template reference variables, simply create a new Angular component or visit an existing one. To create a template reference variable, locate the HTML element that you want to reference and then tag it like so: #myVarName .
Template Reference Variable in angular is used to access all the properties of any element inside DOM. It can also be a reference to an Angular component or directive or a web component. Template Reference Variable can refer to the following – DOM element. Directives.
Use template variables to perform tasks such as respond to user input or finely tune your application's forms. A template variable can refer to the following: a DOM element within a template. a directive or component. a TemplateRef from an ng-template.
You can declare variables in html code by using a template element in Angular 2 or ng-template in Angular 4+. Templates have a context object whose properties can be assigned to variables using let binding syntax. Note that you must specify an outlet for the template, but it can be a reference to itself.
@ViewChild
to access some element in the view.[attr.src]
to creating binding to 'src' attribute of an element.Renderer
if for some reason you need to change the DOM imperatively.See this plunk.
import {Component, Input, ViewChild, Renderer} from 'angular2/core'; @Component({ selector: 'audio-preview', template: ` <audio controls #player [attr.src]="src"> <source [src]="src" type="audio/mpeg"> Your browser does not support the audio element. </audio> ` }) export class AudioPreview { @Input() src: string; @ViewChild('player') player; constructor(public renderer: Renderer) {} ngAfterViewInit() { console.log(this.player); // Another way to set attribute value to element // this.renderer.setElementAttribute(this.player, 'src', this.src); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With