Angular AOT编译错误:类型'AbstractControl'上不存在属性'length'

Ada*_*oza 2 angular

我的应用运行正常.我想使用AOT加速加载应用程序,由于检查FormA,我收到了ngc和AOT设置的编译错误

在打字稿中,我的表单带有FormArray字段:

  private buildForm() {
    this.taskForm = this.formBuilder.group({
      questions: this.formBuilder.array([
      ])
    }
  }
Run Code Online (Sandbox Code Playgroud)

在模板中:

<h3 *ngIf="taskForm.get('questions').length < 1" >Render Decision</h3>
Run Code Online (Sandbox Code Playgroud)

AOT编译:

 node_modules/.bin/ngc -p src/tsconfig-aot.json
Run Code Online (Sandbox Code Playgroud)

错误:

my-component.component.ngfactory.ts:4530:59: Property 'length' does not exist on type 'AbstractControl'.
Run Code Online (Sandbox Code Playgroud)

Fab*_*eul 6

我通过手动将结果转换.get()为a 找到了解决此问题的方法FormArray.这是在Github问题中描述的,但我会在这里重复一遍以获得良好的衡量标准.

在控制器中提供一个getter:

get formArray() {
  // Typecast, because: reasons
  // https://github.com/angular/angular-cli/issues/6099
  return <FormArray>this.form.get('formArray');
}
Run Code Online (Sandbox Code Playgroud)

在您的模板中,您现在可以访问正确的属性和方法FormArray.

例如:

[disabled]="formArray.length > 0 && formArray.at(formArray.length - 1).invalid"
Run Code Online (Sandbox Code Playgroud)