首页 > 解决方案 > 在构建角度应用程序时收到一些警告消息

问题描述

使用命令构建角度应用程序时出现以下错误ng build --prod

错误:

edit.component.html(111,50): Property 'controls' does not exist on type 'AbstractControl'.
edit.component.html(113,54): Property 'controls' does not exist on type 'AbstractControl'

我在下面解释我的代码。

<div *ngIf="storeForm.get('Address').controls['AddressLine1'].invalid && (storeForm.get('Address').controls['AddressLine1'].dirty || storeForm.get('Address').controls['AddressLine1'].touched)"
                                            class="alert alert-danger">
<div *ngIf="storeForm.get('Address').controls['AddressLine1'].errors.required">
                                                    AddressLine1 is required.
     </div>
</div>

this.storeForm = this.fb.group({
      Address: this.fb.group({
        AddressLine1: ['', Validators.required],

      }),

    });

我需要在构建应用程序时忽略这些警告消息,并且我正在使用Angular cli-8.2.1.

标签: angularangular-formbuilder

解决方案


请在下面找到工作解决方案 -

.ts 文件

myForm: FormGroup;

ngOnInit() {
   this.myForm = this.fb.group({
      testimonials: new FormArray([])
    });

    this.getInitialForms();
}

// THIS IS IMPORTANT
// getter  - (accessed as this.testimonialsForm in this file)
get testimonialsForm() { return this.myForm.get('testimonials') as FormArray; }

getInitialForms() {
    this.testimonialsForm.push(
        this.fb.group({
         name: ['', Validators.required],
         designation: ['', Validators.required],
         testimonial_text: ['', Validators.required]
        })
    );
 }

而在模板文件中,而不是

myForm.get('testimonials').controls

用这个

testimonialsForm.controls

我在使用 angular 8 时遇到了同样的错误,它对我来说非常好。

如果您有任何问题,请告诉我。


推荐阅读