多个输入角度6

时间:2018-09-02 20:39:20

标签: angular

我无法将新值发送到后端

在php后端获取“ config”:

0:{id: "basic1", legend: "Basic 1", limit: 3, pos: 1}
1:{id: "basic2", legend: "Basic 2", limit: 3, pos: 2}
2:{id: "basic3", legend: "Basic 3", limit: 3, pos: 3}

设置FormBuilder:

this.categoryForm = this.formBuilder.group({
  pos: ['', Validators.required],
  limit: ['', Validators.required]
});

打印html:

<form [formGroup]="categoryForm" (ngSubmit)="saveCategory()">
    <tr *ngFor="let data of config; let i = index;" class="d-flex">
      <td class="col-1"><i class="material-icons hand" (click)="categoryExcluirOpen(categoryDelete,i)">delete_forever</i></td>
      <td class="col-3"><input type="text" class="form-control" placeholder="_" formControlName="pos" [value]="data.pos" maxlength="7"></td>
      <td class="col-5">{{data.legend}}</td>
      <td class="col-3"><input type="text" class="form-control" placeholder="_" formControlName="limit" [value]="data.limit" maxlength="7"></td>
    </tr>
</form>

保存:

this.categoryService.saveCategory(this.config)
    .subscribe(
      data => {
        this.loading = false;
        console.log(data);
      }, error => {});

我编辑的值不会被带到后端

StackBlitz

1 个答案:

答案 0 :(得分:0)

saveCategory()必须是需要在ComponentClass上定义的方法。不会直接调用您服务的方法。

我假设this.config将成为具有idlegend属性的对象。并且您期望表单中的limitpos

在组件类中添加一个名为saveCategory()的方法。

saveCategory() {
  const configToStore = {
    ...this.config,
    ...this.categoryForm.value
  }
  this.categoryService.saveCategory(configToStore)
    .subscribe(
      data => this.loading = false, 
      error => {}
    );
}