类型'{[key:string]:属性'xxxx'不存在:AbstractControl; }”

时间:2017-03-28 10:02:43

标签: angular typescript angular-components angular2-aot

import { FormGroup } from '@angular/forms';

export class MasterVendorFormContactComponent implements OnInit {
  @Input() formContactGroup: FormGroup;
// rest of the code 
}


<fieldset [formGroup]="formContactGroup" class="master-vendor-form-contact">
  <md-input-container class="master-vendor-form-contact__phone"
                      [dividerColor]="formContactGroup.controls.phone?.valid ? 'default' : 'warn'">
    <input mdInput placeholder="Enter phone" formControlName="phone">
    <md-hint align="end"
             *ngIf="!formContactGroup.controls.phone?.valid">
      Vendor phone number must be in (XXX) XXX-XXXX format
    </md-hint>
  </md-input-container>
  <!-- Rest of the code -->
</fieldset>

在编译此代码时会出现以下错误:

angular_app/src/$$_gendir/app/master-vendors/master-vendor-form-contact/master-vendor-form-contact.component.ngfactory.ts (465,73): Property 'phone' does not exist on type '{ [key: string]: AbstractControl; }'.
angular_app/src/$$_gendir/app/master-vendors/master-vendor-form-contact/master-vendor-form-contact.component.ngfactory.ts (465,143): Property 'phone' does not exist on type '{ [key: string]: AbstractControl; }'.
angular_app/src/$$_gendir/app/master-vendors/master-vendor-form-contact/master-vendor-form-contact.component.ngfactory.ts (476,77): Property 'phone' does not exist on type '{ [key: string]: AbstractControl; }'.
angular_app/src/$$_gendir/app/master-vendors/master-vendor-form-contact/master-vendor-form-contact.component.ngfactory.ts (476,147): Property 'phone' does not exist on type '{ [key: string]: AbstractControl; }'.

我尝试过这种方法:

请勿使用form.controls.controlName,请使用form.get('controlName')

但是我在这里有formContactGroup,所以它似乎不适合我。

1 个答案:

答案 0 :(得分:0)

有两个选项,您可以更新到最新的打字稿版本。由于version 2.2,它们允许带有字符串索引签名的类型的点符号。

或者您可以更改模板,这更有意义,因为这是正确的方法:) documentation

<md-input-container [dividerColor]="formContactGroup.get('phone').valid ? 'default' : 'warn'">