异步管道不适用于主题
问题描述:
我在服务中具有以下BehaviorSubject:
I have the following BehaviorSubject in a service:
isAuthenticated = new BehaviorSubject<boolean>(false);
我正在组件中按如下方式使用它:
And I am using it as follows in a component:
authenticated: Observable<boolean>;
constructor(private accountService: AccountService) { }
ngOnInit() {
this.authenticated = this.accountService.isAuthenticated.asObservable();
}
在模板中,我做类似的事情:
And in the template I do something like :
<li class="login-button" *ngIf="!authenticated | async">
<a (click)="authenticate()">Log in</a>
</li>
<li *ngIf="authenticated | async">
<a>Logged in</a>
</li>
问题是我看不到这两个li
中的任何一个,尽管假设是第一个应该出现,因为我将Subject的初始值赋值为false.
The issue is that I dont see any of the two li
, although the assumption is that the first one should appear since I am assigning the initial value of the Subject to false.
我在做什么错了?
答
我怀疑其操作顺序-您需要在订阅前后加上括号:
I suspect its the order of operations - you need parenthesis around your subscription:
<li class="login-button" *ngIf="!(authenticated | async)">