Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 Form using setValue to reset value to null

Sorry for this question. This is to do with one of my child component which rejecting null, empty string while writing to form.

I have a form control orderNo. When I try to do
this.myForm.controls['orderNo'].setValue = '123';
It works fine and my view is updated with this value.
But the issue is, I am not able to set the control with null or empty string. Whenever I do
this.myForm.controls['orderNo'].setValue = null;
or
this.myForm.controls['orderNo'].setValue = '';
it doesn't reflect on my view. But I could see the form control holds that null value. (ng.probe($0).componentInstance.myForm.controls['orderNo'].value returns null). It is just not being reflected on the view.

Please help me with some thoughts. Thank you.

like image 725
Josf Avatar asked Aug 21 '17 12:08

Josf


2 Answers

You can directly use patchValue to set value of form fields

    this.myForm.patchValue( {'orderNo':null} );
like image 94
Rohan Fating Avatar answered Oct 13 '22 08:10

Rohan Fating


Below resets entire formgroup to null

    this.myForm.reset();
like image 32
rohithd Avatar answered Oct 13 '22 06:10

rohithd