ngOnChanges跟踪先前值和新值?

rca*_*a02 1 angular

在我的有角度的项目中,我试图通过单击下一个/上一个按钮来浏览列表。我有一个复选框,可将显示的小时数从数小时更改为数天。我希望能够在员工之间来回走动,并让价值观正确无误。有人告诉我,可以通过ngOnChanges并跟踪先前和新的值来完成此操作。

这就是我所拥有的,它正在更改新值,但是如何更改以前的值?

 ngOnChanges(changes: { [propName: string]: SimpleChange }) {
    for (let propName in changes) {
        if (propName == "selectedEmployee") {
            if (this.timeVar == "days") {
                this.empInfo[this.selectedEmployee].STDLTD = this.empInfo[this.selectedEmployee].STDLTD * 8;
                this.empInfo[this.selectedEmployee].Uncharged = this.empInfo[this.selectedEmployee].Uncharged * 8;

                this.empInfo[this.selectedEmployee].PTOBase = this.empInfo[this.selectedEmployee].PTOBase * 8;
                this.empInfo[this.selectedEmployee].PTOCarry = this.empInfo[this.selectedEmployee].PTOCarry * 8;
                this.empInfo[this.selectedEmployee].PTOBorrowed = this.empInfo[this.selectedEmployee].PTOBorrowed * 8;
                this.empInfo[this.selectedEmployee].PTOBalance = this.empInfo[this.selectedEmployee].PTOBalance * 8;
                this.empInfo[this.selectedEmployee].PTORequests = this.empInfo[this.selectedEmployee].PTORequests * 8;
                this.empInfo[this.selectedEmployee].PTORemaining = this.empInfo[this.selectedEmployee].PTORemaining * 8;

                this.empInfo[this.selectedEmployee].ETOEarned = this.empInfo[this.selectedEmployee].ETOEarned * 8;
                this.empInfo[this.selectedEmployee].ETORequests = this.empInfo[this.selectedEmployee].ETORequests * 8;
                this.empInfo[this.selectedEmployee].ETORemaining = this.empInfo[this.selectedEmployee].ETORemaining * 8;  
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

cra*_*ash 5

角度文档

ngOnChanges(changes: SimpleChanges) {
  for (let propName in changes) {
    let chng = changes[propName];
    let cur  = JSON.stringify(chng.currentValue);
    let prev = JSON.stringify(chng.previousValue);
  }
}
Run Code Online (Sandbox Code Playgroud)

因此,您将需要.currentValueand .previousValue属性来访问当前值和先前值。