如何以角度观看表单数组中的变化?

时间:2018-09-25 15:47:45

标签: angular typescript

  constructor(private stockService: StockService, private fb: FormBuilder, public dialog: MatDialog, public snackBar: MatSnackBar, private supplierService: SupplierService, private productService: ProductService) { 
    this.stockForm = this.fb.group ({ //form validator for create and update
      _id: [ "" ],
      stockItem: this.fb.array([ this.createItem() ])
    });
  }


  createItem(): FormGroup { //form array for order Items
    return this.fb.group({
      supplierId: [ "", Validators.compose([Validators.required, Validators.pattern(/^[a-f\d]{24}$/i), Validators.minLength(24), Validators.maxLength(24)]) ],
      productId: [ "", Validators.compose([Validators.required, Validators.pattern(/^[a-f\d]{24}$/i), Validators.minLength(24), Validators.maxLength(24)]) ],
      productQuantity: [ "", Validators.compose([Validators.required, Validators.pattern(/^[0-9]\d*$/), Validators.min(1), Validators.max(99999)]) ]
    });
  }

我想注意表单数组中各个元素的变化。我有代码,但它监视所有表单数组的更改。

  onChanges(): void {
    this.stockForm.get('stockItem').valueChanges.subscribe(val => {
    });
  }

1 个答案:

答案 0 :(得分:0)

我们可以像这样绑定值更改:

  1. 表单更改绑定|要调用任何字段值更改

_

onChanges(): void {
  this.myForm.valueChanges.subscribe(val => {
    //DO you stuff
  });
}
  1. 特定字段绑定|会要求进行特定领域的变更

_

onChanges(): void {
  this.myForm.get('FIELD_NAME').valueChanges.subscribe(val => {

  });
}

希望这会对您有所帮助。还要传递字段名称而不是表单名称。