我有以下代碼...爲什麼我的Angular 2 * ngIf不能渲染直到函數完成?
// Pug Template
.notification-header-area.layout-row.layout-align-center-center(*ngIf="notification.message != null", class="{{notification.color}}")
// Inside angular component
private onNotificationStart = (notification) => {
this.notification = notification;
console.log(this.myElement.nativeElement);
if (this.myElement.nativeElement.children.length > 0) {
// TODO: We should probably do this in a more reliable way in case the template changes.
let elm = this.myElement.nativeElement.children[0];
if (notification.fontSize) elm.style.fontSize = notification.fontSize;
if (notification.colorBackground) elm.style.backgroundColor = notification.colorBackground;
if (notification.colorFont) elm.style.color = notification.colorFont;
}
}
的問題是,如果我在控制檯上線調試,瀏覽器不顯示通知DOM元素。 console.log語句在寫出對象時也會丟失它。如果函數完成運行ngIf呈現自己,我看到預期的元素。是否有$超時等值或什麼?我從Web套接字獲取此事件,並嘗試Trouble with *ngIf in Angular 2 (TypeScript),但它沒有奏效。我也無法在我簡單的插件(不使用網絡套接字)中重新創建它,所以我仍在研究演示。
另外,如果我換在這樣它的工作原理超時...
private onNotificationStart = (notification) => {
this.notification = notification;
setTimeout(() => {
console.log(this.myElement.nativeElement);
if (this.myElement.nativeElement.children.length > 0) {
// TODO: We should probably do this in a more reliable way in case the template changes.
let elm = this.myElement.nativeElement.children[0];
if (notification.fontSize) elm.style.fontSize = notification.fontSize;
if (notification.colorBackground) elm.style.backgroundColor = notification.colorBackground;
if (notification.colorFont) elm.style.color = notification.colorFont;
}
})
// if(notification){
// this.myElement.nativeElement.style.backgroundColor =
// }
}
我不明白問題所在。預期的行爲是什麼? –
爲什麼「* ngIf不能渲染直到函數完成」有問題? –
這不具有相同的問題https://plnkr.co/edit/E2o56qn4el7cniofwvZK?p=preview所以我的不同是什麼? – Jackie