Posts

Showing posts with the label Angular Reactive Forms

Angular 7 And Angular Material How To Get The Selected Option Text Of Mat-select Instead Of Its Value

Answer : Sorry for being late to the party. I'm really horrified of reading all answers above... The solution is much easier and direct than any of the proposed answers, as the select component just passes the selected model as part of the selectionChange argument. But first, some corrections to your example. You've declared an interface, so USE IT: export interface FamilyRelation { id: number; type: string; } So, in your constructor: constructor() { this.familyRelationArray=[ { id: 1, type: 'Parent' }, { id: 2, type: 'Sister' } ] } and not what you put in your StackBlitz... Then your view will become this: <mat-select (selectionChange)="onChange($event)" id="family_relation" placeholder="Family Relation"> <mat-option *ngFor="let familyRelation of familyRelationArray;" [value]="familyRelation.id"> {{familyRelation.typ...

Angular Form Builder Vs Form Control And Form Group

Answer : I have gone through the Angular Official Docs and on the Reactive Forms Part I have seen that: The FormBuilder service is an injectable provider that is provided with the reactive forms module. If you read more you see that the form builder is a service that does the same things as form-group, form-control and form-array. The official docs describe it as: Creating form control instances manually can become repetitive when dealing with multiple forms. The FormBuilder service provides convenient methods for generating controls. So basically saying that FormBuilder is a service that is trying to help us reduce boiler-plate code. An example of how FormBuilder is used to reduce boilerplate code can be seen here. To answer the question: So is there any advantage of one over the other way of doing it or is it just preference? Well, there is no technical advantage and whichever code you use all boils down to your preference. This example is here With FormControl: c...