我正在尝试使用 Angular 2
中的*ngIf
以编程方式设置按钮名称
我的html模拟看起来像这样
<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>
我的setDefaultAttachment
方法如下所示:
setDefaultAttachment (id:number){
this.defaultAttaschmentId = id ;
}
但是我收到了这个错误:
EXCEPTION:错误:未捕获(在承诺中):模板解析错误: 分析器错误:绑定不能包含[ngIf defaultAttaschmentId === img.id中第52列的分配?在AttachmentsTabComponent @ 29:126中buttonName ='Default Image':buttonName ='Set As Default'](“pe =”button“class =”btn btn-primary“style =”margin-top:5px;“(click)= “setDefaultAttachment(img.id)”[ERROR - &gt;] * ngIf =“defaultAttaschmentId === img.id?buttonName ='Default Image':buttonName ='Set As Default'”“):AttachmentsTabComponent @ 29:126 * **
答案 0 :(得分:9)
直接使用它
<button type="button" class="btn btn-primary" style="margin-top:5px;" (click)="setDefaultAttachment(img.id)"> {{defaultAttaschmentId === img.id ? 'Default Image' : 'Set As Default'}}</button>