Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 当我';我在我的angular应用程序I'中进行验证;m没有使用类型为'的参数的索引签名;字符串';在类型';AbstractControl[]错误_Javascript_Angular_Typescript - Fatal编程技术网

Javascript 当我';我在我的angular应用程序I'中进行验证;m没有使用类型为'的参数的索引签名;字符串';在类型';AbstractControl[]错误

Javascript 当我';我在我的angular应用程序I'中进行验证;m没有使用类型为'的参数的索引签名;字符串';在类型';AbstractControl[]错误,javascript,angular,typescript,Javascript,Angular,Typescript,当我在angular应用程序中进行验证时,我面临以下错误: src/app/register/register.component.ts:45:39 - error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'AbstractControl[] | { [key: string]: AbstractControl; }'.

当我在angular应用程序中进行验证时,我面临以下错误:

src/app/register/register.component.ts:45:39 - error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'AbstractControl[] | { [key: string]: AbstractControl; }'.
  No index signature with a parameter of type 'string' was found on type 'AbstractControl[] | { [key: string]: AbstractControl; }'.

45             return control?.value === control?.parent?.controls[matchTo].value ? null : {isMatching: true}
                                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
这是产生错误的方法:

matchValues(matchTo: string): ValidatorFn {

   return (control: AbstractControl) => {

        return control?.value === control?.parent?.controls[matchTo].value ? null : {isMatching: true}
       }
    

}

我刚开始使用angular和TypeScript,我不知道如何修复它。任何帮助都将不胜感激。

您必须定义对象的索引类型。在您的例子中,它是一个基于字符串的索引

matchValues(matchTo: string): ValidatorFn {
    return (control: AbstractControl) => {
        return control.value === (control.parent.controls as { [key: string]: AbstractControl })[matchTo].value ? null : { isMatching: true };
    }
}

我只是用//@ts ignore忽略了这一行,它工作正常。

或者你可以禁用tslint规则检查使用/*tslint:disable next line*/错误消失了,但我的模板中的验证不起作用。也许你可以调试你的代码,看看是否有ismatch应用于该控件。在控制台核心面对这个问题。js:6006错误类型错误:无法读取寄存器处未定义的属性“密码”。组件。ts:46。。。。和错误:formGroup需要一个formGroup实例。请把一个传进来。示例:在您的类中:this.myGroup=newformgroup({firstName:newformcontrol()})。。。。由于未检查父控件&&parent.controls是否存在,因此出现未定义错误,因为验证fn将以相同的方式多次执行。我只是创建了一个演示供您参考