Angular 4 patchValue based on index in FormArray
Use FormArray#at + AbstractControl#patchValue: this.items.at(index).patchValue(…); DEMO
Use FormArray#at + AbstractControl#patchValue: this.items.at(index).patchValue(…); DEMO
this.tempForm.disable(); Disables the control. This means the control will be exempt from validation checks and excluded from the aggregate value of any parent. Its status is DISABLED. If the control has children, all children will be disabled to maintain the model. LINK UPDATE Plunker link – https://plnkr.co/edit/CFC4uKpvfE4otJ2PWdkc?p=preview
I faced the same issue and I fixed it by using AfterViewChecked and ChangeDetectorRef: import { AfterViewChecked, ChangeDetectorRef } from ‘@angular/core’ export class ClassName implements AfterViewChecked { constructor(private readonly changeDetectorRef: ChangeDetectorRef) {} ngAfterViewChecked(): void { this.changeDetectorRef.detectChanges(); } }
There’s a pretty straightforward method to do this: markAsTouched. It should be enough to use it on the form group. this.addressForm.markAsTouched() In case you want for some reason to mark all controls manually, they itself have this method available. markAsTouched is a method of the AbstractControl all form elements inherit from. Out of curiosity, you … Read more
To create a disabled FormControl is really simple. 1 – Don’t use disabled attribute in your template; 2 – Instantiate your FormGroup like this: this.formGroup = this.formBuilder.group({ dateJoined: { disabled: true, value: ” } // … }); That being said, although you want to prevent users from typing something in the input, you still want … Read more
You have a property controls in FormArray which is an array of AbstractControl objects. Check the specific documentation for FormArray and you will see they also inherit from AbstractControl like the FormControl you posted. Be aware that in the controls array you can have again inside FormArray or FormGroup objects besides FormControl objects because there … Read more
What you’re looking for is myForm.markAsPristine().
The error means that the form field cannot find a compatible material input inside of it. Do not use <mat-checkbox> inside <mat-form-field>. The following Angular Material components are designed to work inside a <mat-form-field>: <input matNativeControl> & <textarea matNativeControl> (version 7 & newer) <select matNativeControl> (version 7 & newer) <input matInput> & <textarea matInput> (version … Read more
I had the same situation for me to update FormGroup | FormArray at nested level controls. check this out(worked for me): /** * Re-calculates the value and validation status of the entire controls tree. */ function updateTreeValidity(group: FormGroup | FormArray): void { Object.keys(group.controls).forEach((key: string) => { const abstractControl = group.controls[key]; if (abstractControl instanceof FormGroup || … Read more
If you want to get only the status and not the value you can use statusChanges: export class Component { @ViewChild(‘myForm’) myForm; this.myForm.statusChanges.subscribe( result => console.log(result) ); } If you even want data changes, you can subscribe to the valueChanges of the form and check the status of the form using this.myForm.status: export class Component … Read more