验证不会传播到 Angular 中的自定义表单控件 ng-select

DiP*_*Pix 5 typescript angular-ngselect angular angular-reactive-forms

我在 Angular 9 应用程序中使用带有自定义表单控件的响应式表单。

我用自定义表单控件包装了我的ng-select控件。

我在验证方面遇到问题。我将 formControl 设置为必需。文档说cssng-invalid类应该自动设置为 ng-select。但实际上使用自定义表单控件它无法正常工作。css 类未设置,但包装器类已设置。我做错了什么还是这是图书馆的问题?

检查stackblitz: https://stackblitz.com/edit/angular-rmvttg-ex63ka ?file=src/forms-single-select-example.component.html&fbclid=IwAR2robtd_15khTVhmW59lLhn21HOHl_yYTrCWKaPRmfUt1QVvUn3n8V4Vjo

zer*_*ewl -1

您可以通过将FormGroupdown 传递到子组件Input并将其设置为来解决此问题ng-select

<mycontrol formControlName="someControl" [parentFormGroup]="someForm" ></mycontrol>
Run Code Online (Sandbox Code Playgroud)

成分:

export class MyControlComponent implements ControlValueAccessor {

  @Input() parentFormGroup: FormGroup;
...
Run Code Online (Sandbox Code Playgroud)

模板:

<ng-select #mySelect
           [formGroup]="parentFormGroup"
...
Run Code Online (Sandbox Code Playgroud)