好吧,我知道關于這個確切的問題有很多問題,但即使在閱讀了其中一些之后,我也無法解決我的問題。我嘗試了 changeDetectionRef 的解決方案,但要么我做錯了,要么它在我的情況下不起作用。對于我的問題:我正在制作一本預算書,添加交易和其他東西,一切都做得很好,直到我添加了一個余額概覽,它顯示了帳戶的總體余額以及將在本月應用的更改。余額在 ngFor 指令中從父級(budget-book.component)發送到子級(day.component)。我明白了,我遇到的問題是,相同的值被發送給每個孩子,之后接收、更改并發送給下一個孩子。我嘗試在父級中創建一個數組,其中保存每個 balanceChange 并發送前幾天的總和,但這也沒有成功。我也在考慮做一個可觀察的。預算-book.component.html[...]<ul class="days"> <app-day (addEvent)="addTransaction($event)" (removeEvent)="removeTransaction($event)" (balanceEvent)="updateBalance($event)" *ngFor="let day of days; index as i" [date]="sendDate(this.date.getFullYear(), this.date.getMonth(), i + 1)" [transactions]="getTransactions(i)" [balance]="balance"></app-day></ul>[...]預算書.component.ts[...]balance: number;ngOnInit(): void { [...] this.balance = 0;}[...]updateBalance(balanceChange: number) { this.balance += balanceChange;}day.component.html<div class="header-balance-container"> <span [class.positive-number]="balance + balanceChange >= 0" [class.negative-number]="balance + balanceChange < 0" class="balance">{{balance + balanceChange | currency: 'EUR'}}</span> <span [class.positive-number]="balanceChange >= 0" [class.negative-number]="balanceChange < 0 " class="difference" >{{balanceChange | currency: 'EUR'}}</span></div>day.component.ts@Input() balance: number;balanceChange: number;@Output() balanceEvent = new EventEmitter<number>();ngOnChanges(): void { this.balanceChange = 0; if (this.transactions) { this.transactions.forEach((transaction: Transaction) => { this.balanceChange += transaction.value; }); } this.pushBalance(); // here is where i tried adding the changeDetectionRef}[...]pushBalance() { this.balanceEvent.emit(this.balanceChange);}幾天以來我一直在解決這個問題,無法弄清楚如何解決。如果有人可以向我解釋,我缺少什么以及我的解決方案觀點是什么,我會很高興。
Angular - 檢查后表達式已更改
拉莫斯之舞
2022-06-09 16:27:21