我正在尝试定义一个包含动态表单的组件(使用ReactiveForms),用户应该能够添加/删除控件。 控件可以采用多种形式,并且必须在Component之外定义,因此我认为TemplateRef最适合于此。
我正在努力找到一种方法将外部定义的Control绑定到内部Form,通过使用formControlName
以下是实施的开始:
fgets
-
// expandable.component.ts
[...]
@Component({
selector: 'expandable',
templateUrl: 'app/component/common/expandable/expandable.component.html',
styleUrls: ['app/component/common/expandable/expandable.component.css']
})
export class ExpandableComponent {
@ContentChild('childTemplate') childTemplate: TemplateRef<any>;
@Input() children: Array<any>;
public form: FormGroup;
constructor(private _changeDetector: ChangeDetectorRef,
private _formBuilder: FormBuilder) {
this.children = [];
}
public ngOnInit(): void {
this.form = this._formBuilder.group({
children: this._formBuilder.array([])
});
const arrayControl = <FormArray>this.form.controls['children'];
this.children.forEach(child => {
const group = this.initChildForm();
arrayControl.push(group);
});
}
private initChildForm(): AbstractControl {
return this._formBuilder.group({
key: ['Initial Key', [Validators.required]],
value: ['Initial Value', [Validators.required]]
});
}
public addChild(): void {
const control = <FormArray>this.form.controls['children'];
control.push(this.initChildForm());
this._changeDetector.detectChanges();
}
}
尝试在外部定义模板:
<!-- expandable.component.html -->
<form [formGroup]="form">
<div class="form-group">
<div formArrayName="children">
<div *ngFor="let child of form.controls.children.controls; let i=index">
<div [formGroupName]="i">
<template
[ngTemplateOutlet]="childTemplate"
[ngOutletContext]="{ $implicit: child }"></template>
</div>
</div>
</div>
</div>
<a (click)="addChild()">Add Child</a>
</form>
我天真地尝试将formControlName绑定到来自outter的隐式传递的上下文,但没有运气,因为我得到“找不到名字的控件:'value'”。理想情况下,我希望能够将formControlName绑定到 <expandable>
<template #childTemplate>
<input class="form-control"
formControlName="value" />
</template>
</expandable>
,但我也看不到这样做。
对此有何想法?
答案 0 :(得分:4)
是的,这是可能的:
您需要实现ControlValueAccessor
和ngModel
注入的formControlName
接口,如下所示:
@Directive({
selector: 'control',
providers: [
{provide: NG_VALUE_ACCESSOR, multi: true, useExisting: SwitchControlComponent}
]
})
export class SwitchControlComponent implements ControlValueAccessor {
isOn: boolean;
_onChange: (value: any) => void;
writeValue(value: any) {
this.isOn = !!value;
}
registerOnChange(fn: (value: any) => void) {
this._onChange = fn;
}
registerOnTouched() {}
toggle(isOn: boolean) {
this.isOn = isOn;
this._onChange(isOn);
}
}
html:
<expandable>
<template #childTemplate>
<div [formGroup]="form">
<input control class="form-control"
formControlName="value" />
</template>
</div>
</expandable>
进一步阅读:
With new forms api, can't add inputs from child components without adding additional form tags