FormGroup Angular问题(类型中缺少)

时间:2017-12-19 23:39:37

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

当我运行命令 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进行操作,因为您可以看到我实际上在formgroup中添加了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;
 }

1 个答案:

答案 0 :(得分:0)

我在运行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">