Answer the question
In order to leave comments, you need to log in
How to set the start value of a property in Angular 5?
The component has a model with many properties.
public model = {
bool: false,
text: '',
array: [],
number: 0,
obj: {}
};
this.model.reset()
Answer the question
In order to leave comments, you need to log in
this.model = new Model()
or a more time-consuming method, but I don’t think it’s 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];
})
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question