Here is a snippet of code from a class called VibrationElement:
export class VibrationElement {
private _amplitude: number;
get amplitude(): number {
return this._amplitude;
}
set amplitude(amplitude: number) {
console.log("test");
if (this.validate(amplitude)) {
this._amplitude = amplitude;
}
}
}
In my component, I have a method to change the amplitude of an instance of VibrationElement:
export class VibrationPatternEditingComponent {
...
amplitudeChange(vibration: VibrationElement, newValue: any) {
vibration.amplitude = parseInt(newValue);
}
}
The issue I am facing is that the setter of VibrationElement is not being called (no "test" in console) when I try to update the value. However, the value does change without triggering the setter. It's important for me to validate constraints on the amplitude, so the setter is necessary.
I also attempted using
Object.assign(vibration, {amplitude: parseInt(newValue)});
, but that too failed to trigger the setter.
Interestingly, when I use
[(ngModel)]="vibration.amplitude"
within an input form, the setter is triggered. The downside is that the input ends up being a string. This presents a problem when converting it to a number inside the setter since it receives a number parameter, yet somehow it registers as a string when I log typeof amplitude
inside the setter. I'm confused by this behavior and would appreciate any insights or solutions anyone might have.