[[oktatas:web:angular|< Angular]]
====== Angular - Reaktív űrlapok ======
* **Szerző:** Sallai András
* Copyright (c) Sallai András, 2021, 2022, 2023
* Licenc: [[https://creativecommons.org/licenses/by-sa/4.0/|CC Attribution-Share Alike 4.0 International]]
* Web: https://szit.hu
===== Angular űrlapok =====
* sablon-vezérelt űrlap
* reaktív űrlap
==== Sablon-vezérelt űrlap ====
A sablon-vezérelt űrlapra példa:
==== Reaktív űrlap ====
Reaktív űrlap HTML része:
TypeScript része:
name = new FormControl('');
===== Reaktív modul importálása =====
//...
import { ReactiveFormsModule } from '@angular/forms';
//...
imports: [
BrowserModule,
ReactiveFormsModule
],
===== Legyen egy új komponens =====
ng generate component triangle
import { FormControl } from '@angular/forms';
//...
export class TriangleComponent implements OnInit {
base = new FormControl('');
height = new FormControl('');
calcArea() {
let area = Number(this.base.value) *
Number(this.height.value) / 2;
alert("Terület: " + area);
}
}
Háromszög
Tegyük az app.component.html sablonba:
==== Továbbfejlesztve ====
A területet egy input elemben jelenítjük meg:
Háromszög
import { Component, OnInit } from '@angular/core';
import { FormControl } from '@angular/forms';
@Component({
selector: 'app-triangle',
templateUrl: './triangle.component.html',
styleUrls: ['./triangle.component.css']
})
export class TriangleComponent implements OnInit {
base = new FormControl('');
height = new FormControl('');
area = new FormControl('');
constructor() { }
ngOnInit(): void {
}
calcArea() {
let area = Number(this.base.value) *
Number(this.height.value) / 2;
this.area.setValue(area);
}
}
import { NgModule } from '@angular/core';
import { ReactiveFormsModule } from '@angular/forms';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { VehicleComponent } from './vehicle/vehicle.component';
@NgModule({
declarations: [
AppComponent,
VehicleComponent
],
imports: [
BrowserModule,
ReactiveFormsModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
===== Űrlapvezérlők csoportosítása =====
Importáljuk a FromGroup osztályt a triangle.components.ts fájlban.
import { FormControl, FormGroup } from '@angular/forms';
Használat:
triangleForm = new FormGroup( {
base: new FormControl(''),
height: new FormControl(''),
area: new FormControl('')
})
A TypeScript ebben az esetben:
calcArea() {
let area = Number(this.triangleForm.value.base) *
Number(this.triangleForm.value.height) / 2;
this.triangleForm.patchValue({area: area});
}
==== submit() ====
Kattintás helyett most ngSubmit eseményre fogunk űrlapot küldeni.
A form elembe:
A TypeScript fájlban:
onSubmit() {
this.calcArea();
}
calcArea() {
let area = Number(this.triangleForm.value.base) *
Number(this.triangleForm.value.height) / 2;
this.triangleForm.patchValue({area: area});
}
==== Teljes kód ====
Háromszög
import { Component, OnInit } from '@angular/core';
import { FormControl, FormGroup } from '@angular/forms';
@Component({
selector: 'app-triangle',
templateUrl: './triangle.component.html',
styleUrls: ['./triangle.component.css']
})
export class TriangleComponent implements OnInit {
triangleForm = new FormGroup( {
base: new FormControl(''),
height: new FormControl(''),
area: new FormControl('')
});
constructor() { }
ngOnInit(): void {
}
onSubmit() {
this.calcArea();
}
calcArea() {
let area = Number(this.triangleForm.value.base) *
Number(this.triangleForm.value.height) / 2;
this.triangleForm.patchValue({area: area});
}
}
==== Teljes kód átszervezve ====
A triangleForm objektumot az ngOnInit() metódusban készítjük elő.
Háromszög
import { Component, OnInit } from '@angular/core';
import { FormControl, FormGroup } from '@angular/forms';
@Component({
selector: 'app-triangle',
templateUrl: './triangle.component.html',
styleUrls: ['./triangle.component.css']
})
export class TriangleComponent implements OnInit {
triangleForm ! : FormGroup;
constructor() { }
ngOnInit(): void {
this.triangleForm = new FormGroup( {
base: new FormControl(''),
height: new FormControl(''),
area: new FormControl('')
});
}
onSubmit() {
this.calcArea();
}
calcArea() {
let area = Number(this.triangleForm.value.base) *
Number(this.triangleForm.value.height) / 2;
this.triangleForm.patchValue({area: area});
}
}
===== FormBuilder használata =====
A FormBuilder a reaktív űrlapok készítését hivatott megkönnyíteni.
Lássuk a fenti programunkat, csak a TypeScript fájl változtatva:
import { FormControl, FormGroup, FormBuilder } from '@angular/forms';
//..
export class TriangleComponent implements OnInit {
triangleForm ! : FormGroup;
constructor(private formBuilder: FormBuilder) { }
ngOnInit(): void {
this.triangleForm = this.formBuilder.group( {
base: [''],
height: [''],
area: ['']
});
}
===== Érvényesítő beállítása =====
Érvényesítő beállítása a TypeScript fájlban:
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
//...
base: ['', Validators.required],
height: ['', Validators.required],
A sablonfájlban:
Az alap megadása kötelező.
A teljes kód:
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
@Component({
selector: 'app-triangle',
templateUrl: './triangle.component.html',
styleUrls: ['./triangle.component.css']
})
export class TriangleComponent implements OnInit {
triangleForm ! : FormGroup;
constructor(private formBuilder: FormBuilder) { }
ngOnInit(): void {
this.triangleForm = this.formBuilder.group( {
base: ['', Validators.required],
height: ['', Validators.required],
area: ['']
});
}
onSubmit() {
this.calcArea();
}
calcArea() {
let area = Number(this.triangleForm.value.base) *
Number(this.triangleForm.value.height) / 2;
this.triangleForm.patchValue({area: area});
}
}
Háromszög
===== Linkek =====
* https://angular.io/guide/reactive-forms (2021)