L
L
Ler Den2018-05-01 12:23:36
JavaScript
Ler Den, 2018-05-01 12:23:36

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: {}
    };

During the operation of the component, the values ​​may change, but it may be necessary to roll back to the original values. The whole component does not need to be reinitialized, just some properties. Is there such a method in Angular/Typescript/JS ?
for example
this.model.reset()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
Coder321, 2018-05-02
@givemoneybiatch

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 question

Ask a Question

731 491 924 answers to any question