【发布时间】:2017-07-09 10:31:52
【问题描述】:
我有一个通过表单生成器构建表单的组件,现在在该表单中我需要包含一个只有一个输入框作为表单控件的组件。
添加.ts
this.newForm = this.fb.group({
name: ['', [Validators.required]],
surname:['', [Validators.required]],
time: ''
});
“时间”必须作为不同的组件包含在内。
添加.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>
时间.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>
如何将表单控件“时间”作为组件包含在主表单中,以便我可以通过 this.newForm.controls['time'].value 访问值??
【问题讨论】:
-
这是父母和孩子吗?
-
@AJT_82 是的.. 是父子关系
标签: angular components angular2-forms