Я хочу изменить HTML представление с помощью *ngIf
на основе локальной переменной, которая должна меняться в зависимости от переменной, доставленной через наблюдаемую из общего сервиса.
HTML
<div class="login-container" *ngIf="!isAuthenticated">
TypeScript того же компонента:
export class LoginComponent implements OnInit {
authenticationsSubscription;
isAuthenticated: boolean;
constructor(
private authService: AuthServiceService,
private router: Router,
private route: ActivatedRoute){}
getAuth(): Observable<boolean>{
return this.authService.validation();
}
ngOnInit() {
this.authenticationsSubscription = this.authService.validation().subscribe(auth => this.isAuthenticated = auth);
}
}
TypeScript общего сервиса AuthService
:
export class AuthServiceService {
isAuthenticated: boolean;
validation(): Observable<boolean>{
return of(this.isAuthenticated);
}
}
Во время отладки я обнаружил, что переменная isAuthenticated
в LoginComponent не изменяется при изменении переменной isAuthenticated
AuthService. Я также попытался использовать pipe()
и tap()
, что ничего не изменило.
Что я делаю не так?