I have the following code...
// 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;
}
}
The problem is if I debug at the console line, the browser does not show the notification dom element. The console.log statement is also missing it when it writes out the object. If the function completes running the ngIf renders itself and I see the element as expected. Is there a $timeout equivalent or something? I am getting this event from a web socket and I tried Trouble with *ngIf in Angular 2 (TypeScript) but it didn't work. I also couldn't recreate it in my simple plunker (that wasn't using web sockets) so I am still looking into a demo.
Also if I wrap in a timeout like this it works...
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 =
// }
}