angular-reactive-forms

Reactive Forms: How to add new FormGroup or FormArray into an existing FormGroup at a later point in time in Angular 7

旧巷老猫 提交于 2019-11-26 11:36:01
问题 In the other examples at StackOverflow there are many questions about using FormGroups in FormArrays. But my question is the opposite. FormArrays have a push method, that makes many things possible. FormGroups have indeed an addControl method for adding simple FormControls. Afaik FormGroups do not have addFormArray or addFormGroup method. Therefore I need your help. Following situation: this.myForm = this.fb.group({ id: this.fb.control([this.book.id]), // or short form `id: [this.book.id],` }

When to use FormGroup vs. FormArray?

不羁岁月 提交于 2019-11-26 09:19:15
问题 FormGroup: A FormGroup aggregates the values of each child FormControl into one object, with each control name as the key. const form = new FormGroup({ first: new FormControl(\'Nancy\', Validators.minLength(2)), last: new FormControl(\'Drew\'), }); FormArray: A FormArray aggregates the values of each child FormControl into an array. const arr = new FormArray([ new FormControl(\'Nancy\', Validators.minLength(2)), new FormControl(\'Drew\'), ]); When should one be used over the other? 回答1:

Angular 5 FormGroup reset doesn't reset validators

青春壹個敷衍的年華 提交于 2019-11-26 06:06:34
问题 I have a form on my page and when I call FormGroup.reset() it sets the forms class to ng-pristine ng-untouched but FormControl.hasError(...) still returns truthy. What am I doing wrong here? Template <form [formGroup]=\"myForm\" (ngSubmit)=\"submitForm(myForm)\"> <mat-form-field> <input matInput formControlName=\"email\" /> <mat-error *ngIf=\"email.hasError(\'required\')\"> Email is a required feild </mat-error> </mat-form-field> <mat-form-field> <input matInput type=\"password\"

How to disable a input in angular2

混江龙づ霸主 提交于 2019-11-26 05:21:24
问题 In ts is_edit = true to disable... <input [disabled]=\"is_edit==\'false\' ? true : null\" id=\"name\" type=\"text\" [(ngModel)]=\"model.name\" formControlName=\"name\" class=\"form-control\" minlength=\"2\"> I just simply want to disable a input based on true or false . I tried following: [disabled]=\"is_edit==\'false\' ? true : null\" [disabled]=\"is_edit==\'true\'\" [disabled]=\"is_edit\" 回答1: Try using attr.disabled , instead of disabled <input [attr.disabled]="disabled ? '' : null"/> 回答2: