我有订阅的用户对象。如果用户等于某个名称,则不应该显示按钮。我不知道为什么它不会影响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和show delete我得到了预期值,但它没有改变html中的逻辑
答案 0 :(得分:1)
您应该将showDelete
变量声明为组件的一部分,以便可以从模板访问它:
public showDelete: boolean = true;
ngOnInit(): void {
this.authService.getProfile().subscribe(profile => {
this.username = profile.user.name;
if (this.username == "admin") {
this.showDelete = false;
}
});
}
答案 1 :(得分:0)
您无法从模板访问showDelete,因为它是在ngOnInit()中定义的
尝试像这样定义:
class MyClass {
showDelete:boolean;
ngOnInit() {
this.authService.getProfile().subscribe(profile => {
this.username = profile.user.name
if (this.username=="admin"){
this.showDelete = false;
}
}
}