角度:找不到路径为'variable->的控件0-> ID' [英] Angular: Cannot find control with path: 'variable-> 0 -> id'

查看:62
本文介绍了角度:找不到路径为'variable->的控件0-> ID'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试对用户动态添加的div使用FormArray,但找不到用户输入的控件!我总是有错误:

I have been trying to use FormArray for a div that is dynamically added by the user, but I can't find the controls for the user inputs! I always have the error:

错误:找不到路径为'textoAvisos->的控件. 0-> assTipo'

Error: Cannot find control with path: 'textoAvisos -> 0 -> assTipo'

错误:找不到路径为'textoAvisos->的控件. 0-> msgTipo'

Error: Cannot find control with path: 'textoAvisos -> 0 -> msgTipo'

错误:找不到路径为'textoAvisos->的控件. 0-> dataTipo'

Error: Cannot find control with path: 'textoAvisos -> 0 -> dataTipo'

div包含用户需要插入的3个输入,看来控件找不到它们.用户单击按钮后会添加一个新的div,因此为什么它需要动态,但这不是问题.我现在不必担心推入式插入,因为我需要首先验证用户的输入!

The div contains 3 inputs that the user need to insert, which it seems that the control cannot find them. A new div is added after the user click a button, hence why it needs to be dynamic, but that is not the issue. I don't need to worry about the push insert for the moment, as I need to validate the input from the user first!

这是HTML:

<form style="text-align: center;" [formGroup]="janelaAtualizacaoForm" (ngSubmit)="cadastrarJanelaAtualizacao()">
    <div *ngFor="let disparaEmail of disparaEmails; let i=index" formArrayName="textoAvisos" class="ui-g-4" style="margin-right: 10px; border: 1px solid #c8c8c8; border-radius: 5px; min-width: 466.828px;">
                    
        <div [formGroupName]="i">
            <p class="titulo-campo font1 fw700">Assunto:</p>
            <textarea pInputTextarea [rows]="2" formControlName="assTipo" required style="width: 100%; resize:unset; font-size: 18px;"></textarea>

            <p class="titulo-campo font1 fw700">Tipo de Aviso:</p>
            <p-editor [style]="{'height':'300px'}" formControlName="msgTipo" required></p-editor>

            <p class="titulo-campo font1 fw700">Data:</p>
            <p-calendar [readonlyInput]="true" formControlName="dataTipo" dateFormat="dd/mm/yy" showButtonBar="true" [locale]="localeService.getLocale()"[monthNavigator]="true" [yearNavigator]="true" yearRange="2018:2050" required></p-calendar>
        </div>
                    
    </div>
</form>

这是TS:

constructor(
    private janelaAtualizacaoService: JanelaAtualizacaoService,
    private segmentoInfoService: SegmentoInformacaoService,
    private empresaService: EmpresaService,
    private route: ActivatedRoute,
    private router: Router, private fb: FormBuilder, private location: Location,
    private changeDetector: ChangeDetectorRef, private localeService: LocaleService
) {
    this.criarJanelas();
}

criarJanelas() {
    this.janelaAtualizacaoSelecionado = [];
    this.janelaAtualizacaoForm = new FormGroup({
        textoAvisos: new FormArray([
            new FormControl(
                new FormGroup({
                    assTipo: new FormControl('', [Validators.required]),
                    msgTipo: new FormControl('', [Validators.required]),
                    dataTipo: new FormControl('', [Validators.required])
                })
            )
        ])
    });
}

谢谢大家的帮助!

推荐答案

您错误地使用了[formGroupName].在<div [formGroupName]="i">行中,您尝试通过索引i获取formGroupName,该索引将不起作用,因为尚未创建任何以数字为名称的FormGroup.

You're using [formGroupName] incorrectly. In your line with <div [formGroupName]="i">, you are trying to get the formGroupName via the index i, which won't work because you have not created any FormGroups that have a number as a name.

我相信有关反应形式的Angular教程将为您提供帮助,特别是有关FormArrays和动态控件的部分:

I believe the Angular tutorial on reactive forms will help you, specifically the part about FormArrays and dynamic controls: https://angular.io/guide/reactive-forms#dynamic-controls-using-form-arrays

要解决您的问题,您可能需要进行以下更改.

To fix your problem, you probably need do the following changes.

HTML:

<div [formGroupName]="i">更改为<div [formGroup]="textoAvisos.controls[i]">

*ngFor="let disparaEmail of disparaEmails; let i=index"更改为*ngFor="let formGroup of textoAvisos.controls; let i=index"

下面提供了第一个示例更改.

The first example change is provided below.

    <form style="text-align: center;" [formGroup]="janelaAtualizacaoForm" (ngSubmit)="cadastrarJanelaAtualizacao()">
    <div *ngFor="let disparaEmail of disparaEmails; let i=index" formArrayName="textoAvisos" class="ui-g-4" style="margin-right: 10px; border: 1px solid #c8c8c8; border-radius: 5px; min-width: 466.828px;">

        <div [formGroup]="textoAvisos.controls[i]">
            <p class="titulo-campo font1 fw700">Assunto:</p>
            <textarea pInputTextarea [rows]="2" formControlName="assTipo" required style="width: 100%; resize:unset; font-size: 18px;"></textarea>

            <p class="titulo-campo font1 fw700">Tipo de Aviso:</p>
            <p-editor [style]="{'height':'300px'}" formControlName="msgTipo" required></p-editor>

            <p class="titulo-campo font1 fw700">Data:</p>
            <p-calendar [readonlyInput]="true" formControlName="dataTipo" dateFormat="dd/mm/yy" showButtonBar="true" [locale]="localeService.getLocale()"[monthNavigator]="true" [yearNavigator]="true" yearRange="2018:2050" required></p-calendar>
        </div>

    </div>
</form>

打字稿:

textoAvisos中的FormGroup中移除周围的FormControl,并为textoAvisos添加吸气剂.如果没有此吸气剂,您将收到有关未定义textoAvisos的错误.让我们感到震惊的是,我们在formArrayName="textoAvisos中使用了textoAvisos,但是您可以像这样使用textoAvisos,因为formArrayName明确地在janelaAtualizacaoForm上查找formArray.当我们尝试在*ngFor中执行textoAvisos.controls时,会得到一个错误,因为我们实际上在组件类中也没有一个属性也要使用该名称进行绑定,因为textoAvisos仅作为表格.

Remove the surrounding FormControl from your FormGroup in textoAvisos and add a getter for textoAvisos. Without this getter, you will get an error regarding textoAvisos being undefined. What tripped us up was that we were using textoAvisos in formArrayName="textoAvisos, but you are able to use textoAvisos like that because formArrayName explicitly looks for a formArray on the janelaAtualizacaoForm. When we try to do textoAvisos.controls in the *ngFor we get an error because we don't actually have a property in our component class to bind too with that name, since textoAvisos exists only as an element on the janelaAtualizacaoForm form.

constructor(
    private janelaAtualizacaoService: JanelaAtualizacaoService,
    private segmentoInfoService: SegmentoInformacaoService,
    private empresaService: EmpresaService,
    private route: ActivatedRoute,
    private router: Router, private fb: FormBuilder, private location: Location,
    private changeDetector: ChangeDetectorRef, private localeService: LocaleService
) {
    this.criarJanelas();
}

public get textoAvisos() {
    return this.janelaAtualizacaoForm .get('textoAvisos') as FormArray;
}

criarJanelas() {
    this.janelaAtualizacaoSelecionado = [];
    this.janelaAtualizacaoForm = new FormGroup({
        textoAvisos: new FormArray([
            new FormGroup({
                assTipo: new FormControl('', [Validators.required]),
                msgTipo: new FormControl('', [Validators.required]),
                dataTipo: new FormControl('', [Validators.required])
            })
        ])
    });
}

我还没有在现场环境中测试过这些,但是希望它们能够解决您的问题.

I have not tested these in a live environment but hopefully they will solve your problem.

这篇关于角度:找不到路径为'variable-&gt;的控件0-&gt; ID'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆