Angular 2 获取附加到此的所有变量。在控制器中

Angular 2 get all variables attached to this. in controller

我的目标是将 class 变量设置为会话存储值(如果会话存储中存在该值)。否则变量将保持其默认值(在 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);

    }
}

当前 class 中的属性可以按如下方式动态访问和更新:

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);

    }
}