I'm making an Angular 2 web app. I have a model that is comprised of a few key properties, and then several other properties that are computed based off those key values.
All of my properties have getter methods. To keep my computed properties in sync with my key properties, the key properties are changed via setter methods which re-evaluate all of the computed properties. Here's a simplified example:
export class Model{ private _keyValue: number; private _computedValue: number; getKeyValue(): number{ return this._keyValue; } setKeyValue(value: number){ this._keyValue = value; this.evaluateComputedValues(); // This might be time-consuming } getComputedValue(): number{ return this._computedValue; } }
This keeps my model consistent: every time one of the key properties is changed, all of the computed properties are re-computed.
Now I need to figure out how to bind my properties to my component views. It seems like I can present the computed property getters using interpolation:
<div>{{model.getComputedValue()}}</div>
However, I'm not sure what the best way to bind my key properties to input fields would be. All of the examples of using two-way binding seem to use ngModel like this:
<input [(ngModel)]='model.property'>
However, that seems geared towards binding to simple properties. I ideally need two-way binding using my separate getter and setter methods (getKeyValue and setKeyValue).
Is there any built-in way to accomplish this in Angular 2?
You need to use this longer form
<input [ngModel]='model.getProperty()' (ngModelChange)="model.setProperty($event)">
You should be aware that the getXxx() methods will be called every time change detection runs, which can be quite often. Ensure that the getters return the same value (especially for objects the same instance) and ensure the getters don't have side effects, otherwise you'll get "Expression has changed since it was last checked ..." errors.
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