Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access a local variable from the template in the controller in Angular2

Tags:

angular

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");     } } 
like image 366
skovmand Avatar asked Dec 29 '15 19:12

skovmand


People also ask

How do I access the template reference variable in component?

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 .

What is template reference variable in angular?

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.

What is template input variable?

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.

How do I declare a variable in angular 12?

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.


1 Answers

  1. Use @ViewChild to access some element in the view.
  2. Use [attr.src] to creating binding to 'src' attribute of an element.
  3. Use 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);   } } 
like image 129
alexpods Avatar answered Sep 28 '22 06:09

alexpods