【发布时间】:2017-05-24 20:45:18
【问题描述】:
我有我正在订阅的用户对象。如果用户等于某个名称,则不应显示按钮。我不知道为什么它不会影响 html 中的 ngIf。
我的 HTML
<input *ngIf = "showDelete" type = "button" (click) =
"deleteCustomer(customer._id)" value = "Delete" class = "btn btn-danger">
我在 appcomponent.ts 中的 ngOnInit
ngOnInit() {
let showDelete:boolean =true;
this.authService.getProfile().subscribe(profile => {
this.username = profile.user.name
if (this.username=="admin"){
showDelete=false;
}
},
如果我 console.log this.username 并显示删除我得到预期的值,但它不会改变 html 中的逻辑
【问题讨论】:
-
只需将
showDelete变量移到您的ngOnInit()函数之外的组件即可
标签: javascript html angular typescript