How do I change an Angular 2 control from code?
When I do it like this:
control.value = "new value";
I get the following error:
TypeError: Cannot set property value of #<AbstractControl> which has only a getter
You should use the markAsDirty method, like this: control. markAsDirty(); This will also mark all direct ancestors as dirty to maintain the model.
You can subscribe to value changes of a control or the whole form. updateValueAndValidity allows you to modify the value of one or more form controls and the flag allows you to specify if you want this to emit the value to valueChanges subscribers.
You can use the updateValue
method:
control.updateValue("new value");
update:
You can now use setValue
:
control.setValue("new value");
You'll need to cast the AbstractControl
to a Control
before you have access to the updateValue
method:
(<Control>yourControl).updateValue(val);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With