简体   繁体   English

如何在 Angular 表单组中嵌套 ControlValueAccessor 组件

[英]How to nest ControlValueAccessor components in an Angular formgroup

I've got a custom component that implements ControlValueAccessor and correctly responds to updates in the parent component FormGroup and updates that FormGroup when the UI is updated.我有一个自定义组件,它实现 ControlValueAccessor 并正确响应父组件 FormGroup 中的更新,并在 UI 更新时更新该 FormGroup。

Existing App template is like this现有的App模板是这样的

<form-input
   inputType="radio"
   id="method-del"
   formControlName="method"
   inputValue="Delivery"
><label>Delivery</label>
</form-input>

And the form-input:和表单输入:

@Component({
    selector: 'form-input',
    template: `<input #input 
                type="{{ inputType }}" 
                id="{{ id || formControlName }}"
                [value]="inputValue" 
                (blur)="onBlur($event)" 
                (keyup)="onKeyup(input.value)" 
                (focus)="onFocus(input.value)" 
                (change)="onChange($event)" 
              />`,
    providers: [
        {
            provide: NG_VALUE_ACCESSOR,
            useExisting: forwardRef(() => InputComponent),
            multi: true,
        },
    ],
})
export class InputComponent implements ControlValueAccessor, OnInit {
...
    registerOnChange(fn: any): void {
        this.onChangeCallback = fn;
    }

    registerOnTouched(fn: any): void {
        this.onTouchedCallback = fn;
    }

    writeValue(value: any): void {
        this.checked = value === this.inputValue;
    }
}

But I now need to wrap the custom component in another custom component and I can't work out how that should be done.但是我现在需要将自定义组件包装在另一个自定义组件中,但我不知道应该怎么做。 Does the wrapping custom component now also need to implement ControlValueAccessor?包装自定义组件现在是否也需要实现 ControlValueAccessor? If so how should the onChangeCallback in the nested custom component be hooked up to the onChangeCallback in the wrapping custom component?如果是这样,嵌套自定义组件中的 onChangeCallback 应该如何连接到包装自定义组件中的 onChangeCallback? And should both components have a provider for NG_VALUE_ACCESSOR that forwards refs to itself?并且两个组件都应该有一个 NG_VALUE_ACCESSOR 的提供者来将 refs 转发给自己吗? Or is there a fancy way of doing this such that the wrapping component is just a pass through?或者有没有一种奇特的方式来做到这一点,使得包装组件只是一个通过?

-App (<form [formGroup=""]>
 |
  - WrappingInputComponent <-------This needs to be inserted in the chain
    |
     - FormInputComponent
       |
        - <input/>

Pass to the wrapping component the formControl using [formControl]使用 [formControl] 将 formControl 传递给包装组件

//wrappingInputComponent
@Input(control) control;

<form-input inputType="radio" id="method-del"
   [formControl]="control" inputValue="Delivery">
<label>Delivery</label>
</form-input>

And your app component还有你的应用组件

//App component
<form [formGroup]="form">
    <wrapping-component [control]="form.get('method')"><wrapping-component>
</form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM