Currently, I'm utilizing Typescript in conjunction with Angular2, mirroring the structure of the Angular2 Tour of Heroes guide.
There is a specific input field that I aim to associate a change
event with, triggering custom logic whenever the value within the field is altered. In order to facilitate this, it's crucial for me to access the current value of the field before any changes take place. Consequently, employing ngModel
to bind the field would not be ideal as it could potentially overwrite the property prior to retrieving its original value.
This results in something similar to the following setup:
<input (change)="handleChange(myObj, $event)" value={{ myObj.myField }}... />
Subsequently, within the handleChange function:
handleChange(obj: MyObjectClass, e: Event) {
let oldValue: number = obj.myField;
let newValue : number = parseInt(e.target.value);
// Implement necessary logic
obj.myField = newValue;
}
Despite functioning correctly in terms of functionality, an error has been presented by the Typescript compiler which reads
error TS2339: Property 'value' does not exist on type 'EventTarget'.
, specifically referring to the line newValue : number = parseInt(e.target.value);
Are there more efficient methods to achieve the same result?