How to set the starting value of a property in Angular 5?

0 like 0 dislike
6 views
In the component there is a model with many properties.
public model = { bool: false, text: ", array: [], number: 0, obj: {} };

In the process, the components values can change, but sometimes need to do a rollback to their original values. The entire component does not need to reinitialize, only some of the properties. Is there a method in Angular/Typescript/JS ?
For example
this.model.reset()
by | 6 views

1 Answer

0 like 0 dislike
this.model = new Model()
or more time consuming way, but I don't think that is correct
class Model { private _initialValues = { name: 'name', surname: 'surname' } constructor() { } reset() { Object.keys(this._initialValues).forEach(key => { this[key] = this._initialValues[key]; }) } } class Model { constructor() { this.name = 'test' } reset() { const newInstance = new Test(); Object.keys(newInstance).forEach(key => { this[key] = newInstance[key]; }) } }
by

Related questions

0 like 0 dislike
1 answer
asked Apr 27, 2019 by shamansyberia
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
110,608 questions
257,186 answers
0 comments
33,932 users