Reputation: 4685
So, let's say I have two fields in my class for simplicity:
export class SomeClass {
selectedObjects: MyClass[];
fieldToUpdateWhenArrayAboveChange:string;
}
Based on the example above, I would like to populate second field with comma separated values of first field (the array)
'selectedObjects' first field is hookup with UI via 'ngModel'
Is there a way to run custom code when a field changes in Angular 2. In C# classes that can be easily done with setters.
Upvotes: 0
Views: 2972
Reputation: 4685
Well, I could not figure it out with typescript itself, but was able to achieve the desired effect with Angular 2 event binding. I have introduced 'ngModelChange' on the field that was bound to the mentioned array (multiselect input field). When array changes, it triggers the method below, where I parse the array any way I want and populate other typescript fields
(ngModelChange)='updateTitleAndDescription()'
Upvotes: 1