简体   繁体   English

使用 mat-slide-toggle 时没有值访问器错误

[英]No value accessor error when using mat-slide-toggle

I'm using angular reactive forms, the form group is initialized in the parent component and form controls are located in the child components.我正在使用 angular 反应式 forms,表单组在父组件中初始化,表单控件位于子组件中。

The problem is that I getting the following error message when I access the page:问题是我在访问该页面时收到以下错误消息:

ERROR Error: No value accessor for form control with unspecified name attribute

For testing purpose, I tried to replace mat-slide-toggle with input checkbox and the error goes away, why is it complaining with slider, I can't understand.出于测试目的,我尝试用输入复选框替换mat-slide-toggle并且错误消失了,为什么它抱怨 slider,我无法理解。

Here's similar example:这是一个类似的例子:

Parent Module父模块

@NgModule({
    ...
    declarations: [ParentComponent],
    imports: [
        ...,
        MatSlideToggleModule,
    ]
})
export class ParentModule {}

Parent Component父组件

@Component({
    selector: 'app-parent',
    template: `
        <form [formGroup]="myFormGroup">
            <app-child-a [subscribedControl]="$any(myFormGroup.controls.myOption)"></app-child-a>
        </form>
    `
})
export class ParentComponent {
    public myFormGroup: FormGroup = this._fb.group({
        myOption: [true, [Validators.required]],
    });

    constructor(private _fb: FormBuilder) {}
}

Child A Component子 A 组件

@Component({
    selector: 'app-child-a',
    template: `
        <mat-slide-toggle [formControl]="subscribedControl"></mat-slide-toggle>
    `
})
export class ChildAComponent {
    @Input() public subscribedControl!: FormControl;
}

Any ideas?有任何想法吗?

Based on what you provided and considering this is not the original snippet, here are a few things to check:根据您提供的内容并考虑到这不是原始代码段,这里有几件事需要检查:

  1. Are you declaring your child component in your ParentModule ?您是否在ParentModule中声明您的子组件? Or are you importing the appropriate module that contains it?或者您是否正在导入包含它的适当模块? Cause in the provided snippet you don't.因为在提供的代码段中你没有。 (This would probably generate a different error though) (虽然这可能会产生不同的错误)
  2. Are you using the "reserved" name formControl as your child's input property name?您是否使用“保留”名称formControl作为您孩子的输入属性名称? If so change it to something else.如果是这样,请将其更改为其他内容。 Eg @Input() control: FormControl .例如@Input() control: FormControl

On another note, passing the form control itself isn't an elegant solution.另一方面,传递表单控件本身并不是一个优雅的解决方案。

If you want to use your ChildComponent as a reusable custom form control, then your component needs to implement the ControlValueAccessor interface and provide it as such.如果您想将ChildComponent用作可重用的自定义表单控件,则您的组件需要实现ControlValueAccessor接口并提供它。

This way you're basically saying to Angular: "look my custom control follows the rules that every other native form element follows. So you can use it in the form."这样你基本上是在对 Angular 说:“看我的自定义控件遵循所有其他原生表单元素遵循的规则。所以你可以在表单中使用它。”

If you implement this interface and provide your component as a CVA, then you don't need to pass the form control.如果您实现此接口并将组件作为 CVA 提供,则无需传递表单控件。

You can read a detailed explanation in this article .你可以阅读这篇文章的详细解释。

Alternatively, if you simply need to break the form into sub-form components and don't care for re-usability & portability, then you can use this approach .或者,如果您只是需要将表单分解为子表单组件并且不关心可重用性和可移植性,那么您可以使用这种方法

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

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