我有以下代码...为什么我的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