[英]How to change the button property dynamically in angular2
Template,模板,
<div *ngFor="let detail of details" class = "col-sm-12 nopadding">
<a class="button buttonaquacss button-mini button-aqua
text-right pull-right" [ngClass]="{activec: color}"
(click)='sendrequest(button,detail._id)'
#button [ngStyle]="{'background-color': color}">{{buttonname}}
</a>
</div>
My ts,我的 ts
buttonname = 'connect';
sendrequest(button, index): void {
this.http.post('http://localhost:3000/sendrequest', formdata, { headers: headers })
.subscribe(
response => {
if (response.json().status == 'success') {
buttonname = 'pending';
this.color = true;
}
});
}
The proflem is when I click on one button all the button properties are getting changed to pending can anyone give a suggestion please.问题是当我单击一个按钮时,所有按钮属性都将更改为待处理,任何人都可以提出建议。
Because there is only a single buttonname
variable and all buttons bind to the same.因为只有一个buttonname
变量并且所有按钮都绑定到相同的。 You need to bind them to different variables.您需要将它们绑定到不同的变量。 Either you use a different variable name for each button or you use an array of values.要么为每个按钮使用不同的变量名称,要么使用一组值。
<a (click)='send(button,detail._id)' #button>{{buttonname[detail._id]}}</a>
buttonname = {'id1': 'connect', 'id2': 'connect'};
sendrequest(button, index): void {
this.http.post('http://localhost:3000/sendrequest', formdata, { headers: headers })
.subscribe(
response => {
if (response.json().status == 'success') {
buttonname[index] = 'pending';
this.color = true;
}
});
}
update更新
<a (click)='send(button,detail._id)' #button>{{pendingId == detail._id ? 'pending' : 'success'}}</a>
pendingId;
sendrequest(button, index): void {
this.pendingId = index;
this.http.post('http://localhost:3000/sendrequest', formdata, { headers: headers })
.subscribe(
response => {
if (response.json().status == 'success') {
pendingId = null;
this.color = true;
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.