使用此资源,我想在多个嵌套级别上实现formControlName。
Angular 2 - formControlName inside component
假设实际的formGroup位于子formControlName组件之上的3个组件级别,
如果父级组件位于子级旁边,则ControlValueaccessor可以工作。但是,以上(祖父)形式的多个级别不起作用。
是否有Service的替代品,还是有多个输入/输出?还是这些是唯一的方法?
A--> Component with formGroup
B---> Component container
C---> Component container
D ---> Component with FormControlName (should pass to Component A)
组件A将从与此相似的不同子组件收集多个表单控件名称,
InputText.ts
export class InputTextComponent implements AfterViewInit,ControlValueaccessor {
@Input() disabled: boolean;
@Output() saveValue = new EventEmitter();
value: string;
onChange: () => void;
onTouched: () => void;
writeValue(value: any) {
this.value = value ? value : "";
}
registerOnChange(fn: any) {this.onChange = fn}
registerOnTouched(fn: any) {this.onTouched = fn}
setDisabledState(isDisabled) {this.disabled = isDisabled}
}
InputText.html
<input .. />