【问题标题】:FormGroup Angular issue (missing in type)FormGroup Angular 问题(类型缺失)
【发布时间】:2018-02-08 14:12:27
【问题描述】:

当我运行命令 ng serve --prod 我遇到了这个问题:

ng:///home/cristopher_ramirez/Development/angularProjects/lazyPagesRouterTest/src/app/service-order/add-service-order/add-service-order.component.html (28,51) 中的错误:参数“FormGroup”类型的参数不可分配给“ServiceOrder”类型的参数。
“FormGroup”类型中缺少属性“cylinderRefsList”。

请注意,这只发生在我使用 --prod 命令时。

这是我的 HTML 代码(add-service-order.component-html):

<div class="container">
<form [formGroup]="serviceOrderForm" novalidate (ngSubmit)="save(serviceOrderForm)">
<div formArrayName="cylinderRefsList">
  <div class="card cardCylinder">
    <div *ngFor="let cylinder of serviceOrderForm.controls['cylinderRefsList']['controls']; let i=index" class="panel panel-default">
      <div class="card-header">
        <span>Cilindro {{i + 1}}</span>
        <button type="button" class="close" aria-label="Close" *ngIf="serviceOrderForm.controls['cylinderRefsList']['controls'].length > 1"
          (click)="removeCylinder(i)">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="card-body" [formGroupName]="i">
        <app-cylinder [group]="serviceOrderForm.controls['cylinderRefsList']['controls'][i]"></app-cylinder>
      </div>
    </div>
  </div>
</div>

我在关注这个指南https://scotch.io/tutorials/how-to-build-nested-model-driven-forms-in-angular-2,所以你可以看到我实际上在表单组中添加了 cylinderRefsList(我的 TS 文件与指南几乎相同)唯一的区别是我使用模型而不是接口。

编辑 1:这是我的 TS 文件

export class AddServiceOrderComponent implements OnInit {

  public serviceOrderForm: FormGroup;

  errMessage: string;
  model;
  structures: any = [];
  structureName = 'Obras';
  isStructure = false;
  isDate = false;
  websafeKey: any;
 orderDate: any;
 dateTicks = 0;
 role: string;

  constructor(
    private router: Router,
   private structureService: StructureService,
 private serviceOrderService: ServiceOrderService,
 private fb: FormBuilder,
 private userService: UserService
 ) {
 this.structureService.getAllStructures().subscribe(data => {
   this.structures = data;
   console.log(this.structures);
 });
 this.role = this.userService.getRoleUser();
 }

 ngOnInit() {
this.serviceOrderForm = this.fb.group({
  cylinderRefsList: this.fb.array([])
 });
 this.addCylinder();
   }

   initCylinders() {
    return this.fb.group({
     colado: ['', Validators.required],
       ruptura: ['', Validators.required],
     edad: ['', Validators.required],
     rev: ['', Validators.required],
     altura: ['', Validators.required],
     area: ['', Validators.required],
    cargaKn: ['', Validators.required],
   cargaKgf: ['', Validators.required],
    resistenciaKgf: ['', Validators.required],
    resistenciaMpa: ['', Validators.required],
    resistenciaProy: ['', Validators.required],
    resistenciaPorce: ['', Validators.required],
    ubicacion: ['', Validators.required]
  });
  }

  addCylinder() {
   const control = 
 <FormArray>this.serviceOrderForm.controls['cylinderRefsList'];
const addrCtrl = this.initCylinders();
control.push(addrCtrl);
   }

  removeCylinder(i: number) {
   // remove address from the list
    const control = 
 <FormArray>this.serviceOrderForm.controls['cylinderRefsList'];
control.removeAt(i);
  }

  save(model: ServiceOrder) {
      this.orderDate = new Date(this.model.year, this.model.month - 1, 
    this.model.day);
     this. dateTicks = ((this.orderDate.getTime() * 10000) + 
  621356076000000000);
  this.serviceOrderService.postServiceOrder(this.dateTicks, this.websafeKey, 
   model['controls']['cylinderRefsList']['value'] )
  .subscribe(data => {
    if (this.role === 'Admin' || this.role === 'Director') {
      this.router.navigate(['/home/service-order']);
    } else if (this.role === 'Tecnico') {
      this.router.navigate(['/home-tecnician/service-order']);
    } else if (this.role === 'Supervisor') {
      this.router.navigate(['/home-supervisor/service-order']);
    }
  });
  }

  structureInfo(structureName: string, websafeKey: string) {
this.structureName = structureName;
this.websafeKey = websafeKey;
 }

【问题讨论】:

  • 第 28 行是什么?
  • @ChauTran 是
  • 得看看你的 ts 文件
  • 我编辑了我的问题以添加它。
  • 可能当你第一次初始化表单时, cylinderRefsList 的0 元素不在正确的界面中。

标签: angular typescript angular-cli angular-reactive-forms


【解决方案1】:

我在运行ng build --prod 时遇到了同样的问题

看看这个https://github.com/angular/angular-cli/issues/6099

我通过关注解决了它

在你的 ts 文件中

get formData() { return <FormArray>this.serviceOrderForm.get('cylinderRefsList'); }

和你的 html

改变 ngFor

<div *ngFor="let cylinder of formData.contorls; let i=index" class="panel panel-default">

【讨论】:

  • abstractcontrol 问题是我首先解决的问题,这就是为什么我的 html 代码中有括号,我尝试了 TS 文件中 getter 的这种解决方案,但出现了同样的错误(相信我,我在堆栈中提出问题之前进行大量搜索),我几乎可以确定我的错误在 HTML 中,但我找不到它。
  • 你试过这个后是否出现同样的错误?
  • 是的,我尝试了您的解决方案并给了我同样的错误。
猜你喜欢
相关资源
最近更新 更多
热门标签