onKeyUp(event:any) { if(event.key == 'Enter') { console.log('Az Enter lett lenyomva'); } }
ng new app01 cd app01 code . ng g c triangle ng add bootstrap
//... import { ReactiveFormsModule } from '@angular/forms'; //... import: [ ReactiveFormsModule ] //...
<div class="container"> <app-triangle></app-triangle> </div>
@import "bootstrap";
<h2>Háromszög területe</h2> <div class="triangleForm"> <div class="mt-3"> <input type="text" placeholder="alap" [formControl]="base" class="form-control" > </div> <div class="mt-3"> <input type="text" placeholder="magasság" [formControl]="height" (keyup)="onKeyup($event)" class="form-control" > </div> <div class="mt-3" *ngIf="areaVisible"> <label for="area" class="form-label">Terület</label> <input type="text" [formControl]="area" class="form-control" > </div> </div>
import { Component, OnInit } from '@angular/core'; import { FormControl } from '@angular/forms'; @Component({ selector: 'app-triangle', templateUrl: './triangle.component.html', styleUrls: ['./triangle.component.scss'] }) export class TriangleComponent implements OnInit { base = new FormControl(''); height = new FormControl(''); area = new FormControl(''); areaVisible = false; constructor() { } ngOnInit(): void { } onKeyup(event:any) { if (event.key == 'Enter') { this.onKeyEnter(); } } onKeyEnter() { let base = Number(this.base.value); let height = Number(this.height.value); let area = this.calcArea(base, height); this.areaVisible = true; this.area.setValue(area); } calcArea(base: number, height: number) { return base * height / 2; } }