Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

*ngIf alternative to set button text in angular 2?

Tags:

angular

I am trying to set button name programmatically using *ngIf in Angular 2

My html mock up looks like this

 <button type="button" class="btn btn-primary" style="margin-top:5px;" (click)="setDefaultAttachment(img.id)"  *ngIf ="defaultAttaschmentId === img.id ? buttonName ='Default Image' : buttonName ='Set As Default'" > {{buttonName}}</button>

My setDefaultAttachment method looks like this :

setDefaultAttachment (id:number){
   this.defaultAttaschmentId = id ;
}

but I am getting this error :

EXCEPTION: Error: Uncaught (in promise): Template parse errors: Parser Error: Bindings cannot contain assignments at column 52 in [ngIf defaultAttaschmentId === img.id ? buttonName ='Default Image' : buttonName ='Set As Default'] in AttachmentsTabComponent@29:126 ("pe="button" class="btn btn-primary" style="margin-top:5px;" (click)="setDefaultAttachment(img.id)" [ERROR ->]*ngIf ="defaultAttaschmentId === img.id ? buttonName ='Default Image' : buttonName ='Set As Default'""): AttachmentsTabComponent@29:126***

like image 407
kero Avatar asked Jul 28 '16 16:07

kero


Video Answer


1 Answers

use it directly like this

 <button type="button" class="btn btn-primary" style="margin-top:5px;" (click)="setDefaultAttachment(img.id)"> {{defaultAttaschmentId === img.id ? 'Default Image' : 'Set As Default'}}</button>
like image 195
rashfmnb Avatar answered Oct 14 '22 07:10

rashfmnb