這個 stackblitz 演示用于設定元素ngStyle的樣式,如下所示:h1
<h1 [ngStyle]="{'background-color':backgroundColor}"
HelloComponent還使用變化OnPush檢測策略:
@Component({
selector: 'hello',
template: `<h1 [ngStyle]="{'background-color':backgroundColor}">Hello {{name}}!</h1>`,
styles: [`h1 { font-family: Lato; }`],
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush,
})
yellow當視圖中的背景顏色更新為ngAfterViewInit()不更新并且顏色保持為藍色時。
我們如何觸發更改檢測,以便樣式更新?
我試著this.cdr.markForCheck()看看能不能觸發它。
uj5u.com熱心網友回復:
你的 stackblitz 控制臺說ERROR Error: NG0100: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'blue'. Current value: 'yellow;'..
你只是有一個錯字。改變
this.backgroundColor = 'yellow;'
至
this.backgroundColor = 'yellow';
(將分號移到字串值之外)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/515376.html
上一篇:角度驗證反應形式的多個選擇
