Showing posts with label Angular. Show all posts
Showing posts with label Angular. Show all posts

Angular Option field change value

<mat-select [(ngModel)]="model.workRightType" formControlName="workrighttype" (change)="onWorkRightChange($event)" placeholder="Engagement Type">
<mat-option *ngFor="let item of workRightTypes" [value]="item.value">       
  {{ item.title}}
  </mat-option>
  </mat-select>     
</mat-form-field>

onWorkRightChange(event){   
this.selectedWorkRight = event.value;       
}

Reactive from

import { FormGroup, FormBuilder } from '@angular/forms';

form: FormGroup;
model: OrganizationAddModel;

constructor(private formBuilder: FormBuilder, private organizationService: OrganizationService) {
    this.model = new OrganizationAddModel();
}

ngOnInit() {
this.form = this.formBuilder.group({
name:[],
email:[]     
});
}

<form [formGroup]="form">
<input [(ngModel)]="model.name" formControlName="name" matInput placeholder="Name">
  <div *ngIf="form.controls.name.invalid">
{{form.controls.name.errors.message}}
</div>
<input  [(ngModel)]="model.email" formControlName="email" matInput placeholder="Email">
</form>

let controls = this.form.controls;       
for (var field in controls) {         
   controls[field].setErrors({
message: "This is required field"
});
}

Form validation

<form [formGroup]="form">
<input [(ngModel)]="model.name" formControlName="name" matInput placeholder="Name">
  <div *ngIf="form.controls.name.invalid">
{{form.controls.name.errors.message}}
</div>
</form>

let controls = this.form.controls;        
for (var field in controls) {          
   controls[field].setErrors({
message: field + "aaaaa"
});
}