* ngIf alternative to set button text in angular 2?

I am trying to set the button name programmatically using *ngIfin Angular 2

My html layout is as follows

 <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 method is setDefaultAttachmentas follows:

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

but I get this error:

EXCEPTION: Error: impurity (in promise): template analysis errors: Parser error: bindings cannot contain assignments in 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 value '" "): AttachmentsTabComponent @ 29: 126 * **

+4
source share
1 answer

use it directly as follows

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

Source: https://habr.com/ru/post/1649444/


All Articles