ng new taller-viewchild
cd taller-viewchild
ng serve
ng generate component contador
import { Component, ViewChild } from '@angular/core';
@Component({
selector: 'app-contador',
templateUrl: './contador.component.html',
})
export class ContadorComponent {
@ViewChild('inputValue') inputElement!: HTMLInputElement;
contador: number = 0;
incrementar() {
const incremento = Number(this.inputElement.value) || 0;
this.contador += incremento;
}
decrementar() {
const decremento = Number(this.inputElement.value) || 0;
this.contador -= decremento;
}
}
<h2>Contador: </h2>
<input #inputValue type="number" placeholder="Incrementar/Decrementar" />
<button (click)="incrementar()">Incrementar</button>
<button (click)="decrementar()">Decrementar</button>
<h1>Ejemplo de ViewChild y Referencias Locales</h1>
<app-contador></app-contador>