0

I am using formArray for my list of product. I want to get the product_code's value in .ts just like [ngModel] and then manipulate the data. How to get the product_code's value?

<tbody formArrayName="products">
 <tr *ngFor="let phone of productForms.controls; let i=index" [formGroupName]="i">
   <th scope="row">{{i+1}}</th>
    <td><input type="text" class="form-control" placeholder="Product Code" formControlName="product_code"></td>
    <td><input type="text" class="form-control" placeholder="Product Name" formControlName="product_name"></td>
    <td><button type="button" class="btn btn-danger ml-2" (click)="deleteProduct(i)">Remove</button></td>
 </tr>
</tbody>

.ts code:

ngOnInit() {
const product = this.fb.group({
      product_code: [],
      product_name: [],
      product_price: [],
      product_quantity: [1],
      product_total: []
    });

 this.myForm = this.fb.group({
  products: this.fb.array([product]),
 }
}
  get productForms() {
    return this.myForm.get('products') as FormArray;
  }

  addProduct() {
    const product = this.fb.group({
      product_code: [],
      product_name: [],
      product_price: [],
      product_quantity: [1],
      product_total: []
    });

    this.productForms.push(product);
  }

  deleteProduct(i) {
    this.productForms.removeAt(i);
  }

For more clarification see this: enter image description here

When i enter any number in the product_code field i need that value in .ts code which also handles the changes or say subscription.

I want to get the product_code value, perform some function and assign the return value to product_name. I know how to achieve this using ngModel two way bindings, but how to do using FormControl or say Reactiveforms?

2
  • Do you mean something like form.controls['product_name'].value ? Commented Sep 17, 2018 at 9:16
  • Create stackblitz with ngModel that you want to archive. After that i will convert it with Reactiveforms Commented Sep 17, 2018 at 9:21

1 Answer 1

1

There is sample how you can loop through products and get eg. product_quantity value:

const product = this.fb.group({
    product_code: [],
    product_name: [],
    product_price: [],
    product_quantity: [1],
    product_total: []
});

const form = this.fb.group({
    products: this.fb.array([product]),
});

const productsArray = form.controls.products as FormArray;
const productsNumber = productsArray.controls.length;
for (let i = 0; i < productsNumber; i++) {
    const productGroup = productsArray.controls[i] as FormGroup;
    console.log(productGroup.controls.product_quantity.value);
}

@Edit 1 Above sample get value only at the moment when you ask for that.

Here is to get it whenever it changes:

for (let i = 0; i < productsNumber; i++) {
    const productGroup = productsArray.controls[i] as FormGroup;

    productGroup.controls.product_code.valueChanges.subscribe(val => {
        // Whenever value of product code changes update product name
        productGroup.controls.product_name.setValue(val + 'some value');
    });
}

@Edit 2 Modified addProduct method to react for newly added controls:

addProduct() {
    const product = this.fb.group({
        product_code: [],
        product_name: [],
        product_price: [],
        product_quantity: [1],
        product_total: []
    });

    product.controls.product_code.valueChanges.subscribe(val => {
        // Whenever value of product code changes update product name
        product.controls.product_name.setValue(val + 'some value');
    });

    this.productForms.push(product);
}
Sign up to request clarification or add additional context in comments.

6 Comments

This gives all the value of a field. but there is no subscription to this. eg if i change the input it should change without submit
@SurendraSinghChhabra yes, it will give value at the moment, when you want to check over changes you can subscribe to valueChanges.
@SurendraSinghChhabra I edited my answer, check if this is what you want.
@SurendraSinghChhabra yes because in addProduct method you add new set of controls and for them you also need to set up some logic when value changes.
can you please provide that logic :D
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.