2017-05-24 52 views
0

我有用戶對象,我訂閱。如果用戶等於某個名稱,則不應顯示按鈕。我不知道爲什麼它不會影響html中的ngIf。ngOnInit varible change爲什麼不影響角度爲2/4的html?

我的HTML

<input *ngIf = "showDelete" type = "button" (click) = 
"deleteCustomer(customer._id)" value = "Delete" class = "btn btn-danger"> 

我ngOnInit在appcomponent.ts

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中的邏輯

+0

只需移動'showDelete'變量之外你的'ngOnInit()'函數由你組成 –

+0

謝謝!!!!!!! – harryd

回答

0

您應該聲明showDelete變量作爲組件的一部分,以便可以從模板訪問該變量:

public showDelete: boolean = true; 

ngOnInit(): void { 
    this.authService.getProfile().subscribe(profile => { 
     this.username = profile.user.name; 
     if (this.username == "admin") { 
      this.showDelete = false; 
     } 
    }); 
} 
+0

謝謝sooo !!!! – harryd

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; 
     } 
    } 
} 
相關問題