【发布时间】:2018-01-22 15:59:48
【问题描述】:
我有一个结帐页面,用户必须先登录才能继续。 用户已经可以登录。在每种情况下,我都想在组件检测到用户是否登录时显示一个微调器。
check-out.html 代码如下:
<div *ngIf="showSpinner">
<app-spinner></app-spinner>
</div>
<div *ngIf="auth.user | async; then authenticated else guest">
<!-- template will replace this div -->
</div>
<!-- User NOT logged in -->
<ng-template #guest>
<div *ngIf="auth.user == null" class="call-to-action">
login buttons...
</div>
</ng-template>
<!-- User logged in -->
<ng-template #authenticated>
payment staps
</ng-template>
我的结帐组件看起来像:
export class CheckoutComponent implements OnInit {
private showSpinner = true;
constructor(public auth: AuthService,
private router: Router) {
}
ngOnInit() {
this.auth.user.subscribe(user => {
this.showSpinner = false;
});
}
...
但总是显示(和),但我只想加载微调器,然后加载#guest 或#authenticated。如何实现?
如果搜索了很多但发现 ngIf 只能采用 if-else 构造。
【问题讨论】:
标签: angular typescript ng-template