7

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.

2
  • Yeah that's weird, Could you show the code for the form and the view? Commented Aug 21, 2017 at 21:09
  • 1
    Thanks for the comment. Just now realised, this has nothing to do with angular2 formcontrol. I have a child component which sets the value of all the control. And on its WritValue() method, It has a condition if(value) { this.value = value; }. That was rejecting my null and empty string. Commented Aug 21, 2017 at 21:13

2 Answers 2

8

Below resets entire formgroup to null

    this.myForm.reset();
Sign up to request clarification or add additional context in comments.

1 Comment

If you've directives attached to your formcontrols they will not working properly after a reset.
7

You can directly use patchValue to set value of form fields

    this.myForm.patchValue( {'orderNo':null} );

Comments

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.