How to catch the exception when set form group control in angular?

Issue

I have a form group.

@Input() readonly formBuilder: FormBuilder;
public myFormGroup :FormGroup;
this.myFormGroup = this.formBuilder.group({name: null});

Then in somewhere I want to patch the value by the code.

try {
    this.myFormControl.controls.name.setValue('some value');
} catch (e) {
   console.error(e);
}

However it failed to set up the value. My question is that I can’t catch the error. In the debugging I found it went to Subscriber.js and method

SafeSubscribe.prototype._tryOrUnsub = function(fn ,value) {
  try {
    fn.call(this._context, value); // hover it shows value = undefined
 }
   catch (err) {
   this.unsubscribe();
   throw err;
  }
};

So I still don’t know what is wrong.

Solution

Figure it out by myself
The control is a kendo-datepicker; it need to convert a format then it works.

<kendo-datepicker [format]="'MM/dd/yyyy'" formControlName = "name"></kendo-datepicker>

And the ts code

this.myFormControl.controls.name.setValue(new Date('some value));

So the trick is to add new Date().

But why I could not catch the error I still don’t know. For the moment it works out now anyway.

Answered By – Hello

Answer Checked By – Jay B. (AngularFixing Admin)

Leave a Reply

Your email address will not be published.