Angular 2把所有的变量都附加到这个变量上.在控制器

Angular 2 get all variables attached to this. in controller

本文关键字:变量 控制器 2把 Angular      更新时间:2023-09-26

我的目标是将类变量设置为会话存储值,如果该值存在于会话存储中。否则,该变量将保持其默认值(在ngOnInit中设置)。

private getTableSessionItems = () => {
    var tSession = JSON.parse(sessionStorage.getItem('tableSession'));
    if(tSession){
        for (var property in tSession) {
            if (tSession.hasOwnProperty(property)) {
                tSession[property]; //Would like to set values equal here this.property = tSession[property]
            }
        }
        console.log('tableSession is true');
    }
    else{
        this.setTableSessionItems();
        console.log('nope keep trying', tSession);
    }
}

当前类中的属性可以动态访问&更新如下:

private getTableSessionItems = () => {
    var tSession = JSON.parse(sessionStorage.getItem('tableSession'));
    if(tSession){
        for (var property in tSession) {
            if (tSession.hasOwnProperty(property)) {
                this[property] = tSession[property];
            }
        }
        console.log('tableSession is true');
    }
    else{
        this.setTableSessionItems();
        console.log('nope try again', tSession);
    }
}