This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Directive({ | |
| selector: '[formControl]' | |
| }) | |
| export class TrimDirective implements OnInit { | |
| constructor( | |
| private ngControl: NgControl, | |
| ) { } | |
| ngOnInit() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| interface RawFormValue { | |
| firstName: string; | |
| lastName: string; | |
| age: number; | |
| } | |
| function toRawFormValue<T extends RawFormValue>(serverData: T): RawFormValue { | |
| return { | |
| firstName: serverData.firstName, | |
| lastName: serverData.lastName, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Component({ | |
| selector: 'my-app', | |
| templateUrl: './app.component.html', | |
| }) | |
| export class AppComponent { | |
| form = this.formBuilder.group({ | |
| firstName: [''], | |
| lastName: [''], | |
| age: [''], | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Component({ | |
| selector: 'my-app', | |
| templateUrl: './app.component.html', | |
| }) | |
| export class AppComponent { | |
| form = this.formBuilder.group({ | |
| firstName: [''], | |
| lastName: [''], | |
| age: [''], | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <custom-component formControlName="controlName"></custom-component> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export class AppComponent { | |
| // rest of the component implementation is ommitted for brevity | |
| submit() { | |
| if (this.form.valid) { | |
| const article = new ArticleDTO(this.form.value as RawFormValue); | |
| // now send the article DTO to the backend using one of your services | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export class ArticleDTO { | |
| title: string; | |
| tags: string; | |
| date: string; | |
| referenceIds: number[]; | |
| constructor(formValue: RawFormValue) { | |
| this.title = formValue.title; | |
| this.tags = formValue.tags.join(','); | |
| this.date = formValue.date.toISOString(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <form [formGroup]="form"> | |
| <div> | |
| <label for="firstName">First Name</label> | |
| <input formControlName="firstName" id="firstName"/> | |
| <span *ngIf="controls.lastName.touched && controls.firstName.hasError('required')" class="errors"> | |
| Field is required | |
| </span> | |
| </div> | |
| <div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Component({ | |
| selector: 'my-app', | |
| templateUrl: './app.component.html', | |
| }) | |
| export class AppComponent { | |
| form = this.formBuilder.group({ | |
| firstName: [''], | |
| lastName: [''], | |
| age: [''], | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <form [formGroup]="form"> | |
| <div> | |
| <label for="firstName">First Name</label> | |
| <input formControlName="firstName" id="firstName"/> | |
| <span *ngIf="form.get('firstName').touched && form.get('firstName').hasError('required')" class="errors"> | |
| Field is required | |
| </span> | |
| </div> | |
| <div> |