I have a common mistake in angular 4 or 2, it is when I initialize a variable in subscribe it has delay. The problem is when I use that variable in html as *ngIf probably it first render that variable in html and at that time errors it is undefined. What is the best solution except setTimeout for this delay?
this is my html:
<a *ngIf="currentUser.type==0" class="tooltip-icon menu-navbar-item" (click)="goListProperty()">
and this is my component:
import {Component, OnInit, OnChanges, EventEmitter, Output, Input,
ViewChild} from '@angular/core';
import {AuthenticationService} from "../../services/authentication.service";
import {LoginService} from "../../services/login-service.service";
import {SignUpService} from "../../services/signup-service.service";
import {Subscription} from 'rxjs';
import {Router} from '@angular/router';
@Component({
selector: 'header-component',
templateUrl: './header.component.html',
})
export class HeaderComponent implements OnInit {
private subscription: Subscription;
menu: boolean;
index: number;
menuButton: number;
user: any;
isOpen = false;
currentUser: any;
loading: boolean;
constructor(private authentication: AuthenticationService, private
signUpService: SignUpService,
private loginService: LoginService, private router: Router) {
this.user = AuthenticationService.check();
}
ngOnInit() {
if (this.user) {
this.currentUser = JSON.parse(localStorage.getItem('currentUser'));
console.log(this.currentUser.type);
this.subscription = this.authentication.notifyObservable$.subscribe(res =>
{
this.currentUser = res;
console.log(this.currentUser.type);
});
}
this.subscription = this.loginService.notifyUserLoginObservable$.subscribe((res) => {
console.log(res);
this.user = res;
});
this.index = 1;
}
thank you for helping :)