Include a component as FormControl Angular2

谁说我不能喝 提交于 2019-12-07 12:36:43

问题


I have component that has form built through form builder and now in that form i need to include a component that has only one input box as a form control.

add.ts

this.newForm = this.fb.group({
      name: ['', [Validators.required]],
      surname:['', [Validators.required]],
      time: ''
    });

"time" has to be included as a different component.

add.html

<div class="form-group">
            <label class="col-md-3 control-label" for="name">{{'forms.newForm.label.configuration.name' | translate}}</label>  
            <div class="col-md-3">
              <input type="text" formControlName="name" placeholder="placeholder" 
                  class="form-control input-md" type="text">
              <small *ngIf="!newForm.controls.name.valid && newForm.controls.name.dirty" class="text-danger">
                Name is required.
              </small>
            </div>

            <label class="col-md-3 control-label" for="surname">{{'forms.newForm.label.configuration.name' | translate}}</label>  
            <div class="col-md-3">
              <input type="text" formControlName="surname" placeholder="placeholder" 
                  class="form-control input-md" type="text">
              <small *ngIf="!newForm.controls.surname.valid && newForm.controls.name.dirty" class="text-danger">
                Surname is required.
              </small>
            </div>
          </div>

time.html

<div>
  <div class="col-md-7">
    <input class="form-control input-md" type="text" (keydown)="eventHandler($event)" maxlength="11">
    <small *ngIf="!validTime" class="text-danger">
      Invalid time
    </small>
  </div>
</div>

How to i include form control "time" as a component in the main form so i can access the value through this.newForm.controls['time'].value??


回答1:


A single Form Control cannot be passed separately to the child component, it has to be passed within a formGroup. So add a group (here named timeGroup) to your form:

this.newForm = this.fb.group({
      name: ['', [Validators.required]],
      surname:['', [Validators.required]],
      timeGroup: this.fb.group({
        time: ''
      })
});

In your parent html pass the formGroup to your child:

<time-comp [timeGroup]="newForm.controls.timeGroup"></time-comp>

and child template:

<div [formGroup]="timeGroup">
  <input formControlName="time">
</div>

And do not forget to mark the formgroup received from parent with Input:

@Input() timeGroup: FormGroup;

Then you can access the time value (in your parent) with:

this.newForm.get('timeGroup').get('time').value;

Also notice that you do not need any events, like keyup, the changes will be catched by the parent without them.

Sample Plunker




回答2:


As of Angular 8, it is possible to pass FormControl as a @Input to a child component without having to put it inside a formGroup and it would still work with two-way binding. An example.. https://angular-v2yaaw.stackblitz.io



来源:https://stackoverflow.com/questions/42339589/include-a-component-as-formcontrol-angular2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!