oktatas:web:angular:angular_reaktiv_urlapok
Különbségek
A kiválasztott változat és az aktuális verzió közötti különbségek a következők.
Előző változat mindkét oldalonElőző változatKövetkező változat | Előző változat | ||
oktatas:web:angular:angular_reaktiv_urlapok [2025/03/02 16:47] – [Reaktív modul importálása] admin | oktatas:web:angular:angular_reaktiv_urlapok [2025/03/02 16:50] (aktuális) – admin | ||
---|---|---|---|
Sor 32: | Sor 32: | ||
name = new FormControl('' | name = new FormControl('' | ||
</ | </ | ||
- | ===== Reaktív modul importálása ===== | ||
+ | ===== Reaktív modul importálása ===== | ||
<code javascript src/ | <code javascript src/ | ||
//... | //... | ||
- | import { ReactiveFormsModule } from ' | + | import { |
+ | FormControl, | ||
+ | FormsModule, | ||
+ | | ||
+ | } from ' | ||
//... | //... | ||
imports: [ | imports: [ | ||
Sor 46: | Sor 50: | ||
- | ===== Legyen egy új komponens ===== | ||
- | ng generate component triangle | + | ===== Űrlap ===== |
- | |||
- | <code javascript src/ | ||
- | import { FormControl } from ' | ||
- | //... | ||
- | export class TriangleComponent implements OnInit { | ||
- | base = new FormControl('' | ||
- | height = new FormControl('' | ||
- | |||
- | calcArea() { | ||
- | let area = Number(this.base.value) * | ||
- | Number(this.height.value) / 2; | ||
- | alert(" | ||
- | } | ||
- | | ||
- | } | ||
- | |||
- | </ | ||
- | |||
- | |||
- | <code html src/ | ||
- | < | ||
- | |||
- | <label for=" | ||
- | <input type=" | ||
- | <br> | ||
- | |||
- | <label for=" | ||
- | <input type=" | ||
- | <br> | ||
- | |||
- | <button (click)=" | ||
- | </ | ||
- | |||
- | Tegyük az app.component.html sablonba: | ||
- | |||
- | <code javascript src/ | ||
- | < | ||
- | </ | ||
- | |||
- | |||
- | |||
- | ==== Továbbfejlesztve ==== | ||
- | |||
- | A területet egy input elemben jelenítjük meg: | ||
- | |||
- | <tabbox triangle.component.html> | ||
- | <code html src/ | ||
- | < | ||
- | |||
- | <label for=" | ||
- | <input type=" | ||
- | <br> | ||
- | |||
- | <label for=" | ||
- | <input type=" | ||
- | <br> | ||
- | |||
- | <button type=" | ||
- | <br> | ||
- | |||
- | < | ||
- | <input type=" | ||
- | <br> | ||
- | </ | ||
- | |||
- | <tabbox triangle.component.ts> | ||
- | |||
- | <code javascript src/ | ||
- | import { Component, OnInit } from ' | ||
- | import { FormControl } from ' | ||
- | |||
- | @Component({ | ||
- | selector: ' | ||
- | templateUrl: | ||
- | styleUrls: [' | ||
- | }) | ||
- | 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); | ||
- | } | ||
- | } | ||
- | </ | ||
- | |||
- | <tabbox app.component.html> | ||
- | <code html src/ | ||
- | < | ||
- | </ | ||
- | |||
- | |||
- | <tabbox app.module.ts> | ||
- | |||
- | <code javascript src/ | ||
- | import { NgModule } from ' | ||
- | import { ReactiveFormsModule } from ' | ||
- | import { BrowserModule } from ' | ||
- | |||
- | import { AppComponent } from ' | ||
- | import { VehicleComponent } from ' | ||
- | |||
- | @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. | ||
- | |||
- | <code javascript> | ||
- | import { FormControl, | ||
- | </ | ||
- | |||
- | Használat: | ||
- | |||
- | <code javascript> | ||
- | triangleForm = new FormGroup( { | ||
- | base: new FormControl('' | ||
- | height: new FormControl('' | ||
- | area: new FormControl('' | ||
- | }) | ||
- | </ | ||
- | |||
- | A TypeScript ebben az esetben: | ||
- | <code javascript> | ||
- | calcArea() { | ||
- | let area = Number(this.triangleForm.value.base) * | ||
- | Number(this.triangleForm.value.height) / 2; | ||
- | | ||
- | this.triangleForm.patchValue({area: | ||
- | } | ||
- | </ | ||
- | |||
- | ==== submit() ==== | ||
- | |||
- | Kattintás helyett most ngSubmit eseményre fogunk űrlapot küldeni. | ||
- | |||
- | A form elembe: | ||
<code html> | <code html> | ||
- | < | + | <input type=" |
+ | class=" | ||
+ | id="name" | ||
+ | [formControl]="name"> | ||
- | <!-- ... --> | ||
- | <button | + | <input type="text" |
+ | class=" | ||
+ | id=" | ||
+ | [formControl]=" | ||
- | </code> | + | <input type=" |
+ | class=" | ||
+ | id=" | ||
+ | [formControl]=" | ||
- | A TypeScript fájlban: | ||
- | <code javascript> | ||
- | onSubmit() { | ||
- | this.calcArea(); | ||
- | } | ||
- | calcArea() { | ||
- | let area = Number(this.triangleForm.value.base) * | ||
- | Number(this.triangleForm.value.height) / 2; | ||
- | | ||
- | this.triangleForm.patchValue({area: | ||
- | } | ||
</ | </ | ||
- | + | A form előkészítése: | |
- | ==== Teljes kód ==== | + | |
- | + | ||
- | <tabbox triangle.component.html> | + | |
- | + | ||
- | <code html> | + | |
- | < | + | |
- | + | ||
- | + | ||
- | <form [formGroup]=" | + | |
- | <label for=" | + | |
- | <input type=" | + | |
- | < | + | |
- | + | ||
- | <label for=" | + | |
- | <input type=" | + | |
- | < | + | |
- | + | ||
- | <button type=" | + | |
- | < | + | |
- | + | ||
- | < | + | |
- | <input type=" | + | |
- | < | + | |
- | </ | + | |
- | + | ||
- | + | ||
- | </ | + | |
- | + | ||
- | + | ||
- | <tabbox triangle.component.ts> | + | |
<code javascript> | <code javascript> | ||
- | import { Component, OnInit } from ' | + | export class AppComponent |
- | import { FormControl, | + | |
- | + | city = new FormControl('' | |
- | @Component({ | + | |
- | selector: ' | + | |
- | templateUrl: | + | |
- | styleUrls: [' | + | |
- | }) | + | |
- | export class TriangleComponent implements OnInit | + | |
- | + | ||
- | | + | |
- | base: 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: | + | |
- | } | + | |
} | } | ||
- | |||
</ | </ | ||
- | <tabbox app.component.html> | + | ===== Kattintás ===== |
- | + | ||
- | <code html5> | + | |
- | < | + | |
- | </ | + | |
- | + | ||
- | + | ||
- | </ | + | |
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | ==== Teljes kód átszervezve | + | |
- | + | ||
- | A triangleForm objektumot az ngOnInit() metódusban készítjük elő. | + | |
- | + | ||
- | <tabbox triangle.component.html> | + | |
<code html> | <code html> | ||
- | < | + | <form (ngSubmit)=" |
- | + | ||
- | + | ||
- | < | + | |
- | <label for=" | + | |
- | <input type=" | + | |
- | < | + | |
- | + | ||
- | <label for=" | + | |
- | <input type=" | + | |
- | < | + | |
- | + | ||
- | <button type=" | + | |
- | < | + | |
- | + | ||
- | < | + | |
- | <input type=" | + | |
- | < | + | |
- | </ | + | |
- | + | ||
</ | </ | ||
- | |||
- | |||
- | <tabbox triangle.component.ts> | ||
<code javascript> | <code javascript> | ||
- | import { Component, OnInit } from ' | ||
- | import { FormControl, | ||
- | |||
- | @Component({ | ||
- | selector: ' | ||
- | templateUrl: | ||
- | styleUrls: [' | ||
- | }) | ||
- | export class TriangleComponent implements OnInit { | ||
- | |||
- | |||
- | triangleForm ! : FormGroup; | ||
- | |||
- | constructor() { } | ||
- | |||
- | ngOnInit(): void { | ||
- | this.triangleForm = new FormGroup( { | ||
- | base: new FormControl('' | ||
- | height: new FormControl('' | ||
- | area: new FormControl('' | ||
- | }); | ||
- | } | ||
- | |||
onSubmit() { | onSubmit() { | ||
- | | + | |
- | } | + | |
- | calcArea() { | + | |
- | let area = Number(this.triangleForm.value.base) * | + | } |
- | | + | |
- | | + | |
- | | + | |
- | } | + | |
- | } | + | |
</ | </ | ||
- | <tabbox app.component.html> | + | Kimenet: |
- | + | < | |
- | < | + | Mari |
- | < | + | Pécs |
+ | 391 | ||
</ | </ | ||
+ | ===== Teljes kód ===== | ||
- | </tabbox> | + | <code html src/app/ |
+ | <form (ngSubmit)=" | ||
+ | <div class=" | ||
+ | <label for=" | ||
+ | <input type=" | ||
+ | class=" | ||
+ | id=" | ||
+ | [formControl]=" | ||
+ | </ | ||
- | ===== FormBuilder használata ===== | + | <div class=" |
- | + | < | |
- | A FormBuilder a reaktív űrlapok készítését hivatott megkönnyíteni. | + | |
- | Lássuk a fenti programunkat, | + | |
- | + | | |
- | <code javascript src/ | + | [formControl]=" |
- | import { FormControl, | + | |
- | //.. | + | |
- | + | ||
- | export class TriangleComponent implements OnInit { | + | |
- | + | ||
- | triangleForm ! : FormGroup; | + | |
- | + | ||
- | constructor(private formBuilder: | + | |
- | + | ||
- | ngOnInit(): void { | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | }); | + | |
- | | + | |
+ | <div class=" | ||
+ | <label for=" | ||
+ | <input type=" | ||
+ | class=" | ||
+ | id=" | ||
+ | [formControl]=" | ||
+ | </ | ||
+ | <button type=" | ||
+ | class=" | ||
+ | Küld | ||
+ | </ | ||
+ | | ||
+ | </ | ||
</ | </ | ||
- | + | <code javascript | |
- | + | import { Component } from ' | |
- | ===== Érvényesítő beállítása ===== | + | import { FormControl, FormsModule, ReactiveFormsModule |
- | + | ||
- | Érvényesítő beállítása a TypeScript fájlban: | + | |
- | + | ||
- | <code javascript> | + | |
- | import { FormGroup, FormBuilder, | + | |
- | //... | + | |
- | base: ['', | + | |
- | height: ['', | + | |
- | </ | + | |
- | + | ||
- | A sablonfájlban: | + | |
- | <code html> | + | |
- | <input type=" | + | |
- | <span *ngIf=" | + | |
- | Az alap megadása kötelező. | + | |
- | </ | + | |
- | </ | + | |
- | + | ||
- | + | ||
- | A teljes kód: | + | |
- | + | ||
- | <tabbox triangle.component.ts> | + | |
- | <code javascript> | + | |
- | import { Component, OnInit | + | |
- | import { FormGroup, FormBuilder, Validators | + | |
@Component({ | @Component({ | ||
- | selector: 'app-triangle', | + | selector: 'app-root', |
- | templateUrl: | + | standalone: true, |
- | | + | imports: [FormsModule, |
+ | templateUrl: | ||
+ | | ||
}) | }) | ||
- | export class TriangleComponent implements OnInit | + | export class AppComponent |
- | + | | |
- | | + | |
- | + | | |
- | | + | |
- | + | ||
- | | + | |
- | this.triangleForm | + | |
- | base: ['' | + | |
- | height: ['', | + | |
- | area: ['' | + | |
- | }); | + | |
- | } | + | |
onSubmit() { | onSubmit() { | ||
- | | + | |
- | } | + | |
- | calcArea() { | + | |
- | let area = Number(this.triangleForm.value.base) * | + | } |
- | | + | |
- | | + | |
- | | + | |
- | } | + | |
} | } | ||
- | </ | ||
- | |||
- | |||
- | <tabbox triangle.component.html> | ||
- | |||
- | <code html> | ||
- | < | ||
- | |||
- | |||
- | <form [formGroup]=" | ||
- | <label for=" | ||
- | <input type=" | ||
- | <span *ngIf=" | ||
- | Az alap megadása kötelező. | ||
- | </ | ||
- | <br> | ||
- | | ||
- | <label for=" | ||
- | <input type=" | ||
- | <span *ngIf=" | ||
- | A magasság megadása kötelező. | ||
- | </ | ||
- | <br> | ||
- | | ||
- | <button type=" | ||
- | <br> | ||
- | | ||
- | < | ||
- | <input type=" | ||
- | <br> | ||
- | </ | ||
- | |||
</ | </ | ||
Sor 517: | Sor 167: | ||
- | </ | ||
- | |||
- | |||
- | |||
- | ===== Linkek ===== | ||
- | * https:// |
oktatas/web/angular/angular_reaktiv_urlapok.1740930429.txt.gz · Utolsó módosítás: 2025/03/02 16:47 szerkesztette: admin