Home > Back-end >  How to pass default value as 0 but not to reflect in HTML
How to pass default value as 0 but not to reflect in HTML

Time:05-11

I am working in angular and I am passing 0 as default value in reactive form formControl. The default value is getting reflected in the HTML input. I dont want to show default value in HTML input field at first.

How can I achieve that ?

carnumbers: new FormControl('0', [Validators.minLength(1), Validators.maxLength(2), Validators.pattern("^[0-9]*$")])

Note: the default value must be 0

CodePudding user response:

If you don't want show default value in View, you should use setValue or patchValue on formControl and pass emitModelToViewChange:false options to it.

 carnumbers = new FormControl(null, [Validators.minLength(1), 
     Validators.maxLength(2), Validators.pattern("^[0-9]*$")]);

  ngAfterViewInit(){ 
      this.carnumbers.setValue(0,{emitModelToViewChange:false});
  } 

Working Simple Example

CodePudding user response:

Use null or '' and change that to your default values as a preprocess step before submitting the form.

  • Related