This is beginners question.. I have been reading Angular2 documentation, I came across example in Hierarchical Dependency Injectors chapter where Restore service is used to make save/cancel functionality available for editing.
This is the service:
export class RestoreService<T> {
originalItem: T;
currentItem: T;
setItem (item: T) {
this.originalItem = item;
this.currentItem = this.clone(item);
}
getItem () :T {
return this.currentItem;
}
restoreItem () :T {
this.currentItem = this.originalItem;
return this.getItem();
}
clone (item: T) :T {
return JSON.parse(JSON.stringify(item));
}
}
I was so excited about it so I tried it myself! First I'm setting the values like this:
ngAfterContentInit(){
this.metadata = {
languages: this.selected_languages,
countries: this.selected_countries,
international: false
}
}
set metadata(metadata: CvMetadata){
this._restoreService.setItem(metadata);
}
get metadata(): CvMetadata{
return this._restoreService.getItem();
}
After that I'm changing metadata properties values with ngModel
for example: [(ngModel)]="metadata.languages[0]"
The question:
For my sruprise When I update the metadata property value with ngModel it works - the currentItem has changed and the orginalItem has not! What I do not understand is how is that possible? I thought that ngModel would use setter for setting the metadata properties. But the setter is called only once when I set the original data. How does the ngModel know that it should change only the currentItem not he originalItem?
Is this the black magic?
I know.. I only need someone to explain this, but unfortunately or fortunately there are only you guys!
Thanks!