admin管理员组文章数量:1400823
I am trying to disable the button after a add button has been clicked. For sake of simplicity I am not adding details just the code that is cause the issue.
<div *ngFor="let n of records">
<span>{{n.name}}</span>
<span>{{n.location}}</span>
<button (click)="addtomainrecord(n)" [disabled]="disablebutton">add</button>
</div>
In my ponent I have declared
disablebutton:boolean=false;
//later in my code
addtomainrecord(record) {
this.disablebutton=true;
//rest of the code follows
}
The issue I am facing is that once I click add button first time, all the buttons are disabled, while I want to just disable the button of row that I just clicked.
How can it be fixed?
I am trying to disable the button after a add button has been clicked. For sake of simplicity I am not adding details just the code that is cause the issue.
<div *ngFor="let n of records">
<span>{{n.name}}</span>
<span>{{n.location}}</span>
<button (click)="addtomainrecord(n)" [disabled]="disablebutton">add</button>
</div>
In my ponent I have declared
disablebutton:boolean=false;
//later in my code
addtomainrecord(record) {
this.disablebutton=true;
//rest of the code follows
}
The issue I am facing is that once I click add button first time, all the buttons are disabled, while I want to just disable the button of row that I just clicked.
How can it be fixed?
Share Improve this question edited Apr 7, 2019 at 5:06 Vega 28.7k28 gold badges120 silver badges145 bronze badges asked Apr 7, 2019 at 3:32 J. DavidsonJ. Davidson 3,31714 gold badges56 silver badges105 bronze badges3 Answers
Reset to default 5You can add a new property to each item of array and check this property for disable item:
Component.ts
myArray:Array<{firstName:string,lastName}>=[]; // there isn't isDisabled in this model
doSomething(item){
item.isDisabled=true;
// do something
}
Component.html:
<div *ngFor="let item of myArray">
<button (click)="doSomething(item)" [disabled]="item.isDisabled">Register</button>
</div>
I hope this helps you.
If you have a the "ownership" for records
array, you can add an other key-value pair, say 'disabled', otherwise you can create a parallel array disablebutton
of the same length as records:
disablebutton = [false,false,...] // better be done in a for loop or with array methods in your code
In the template, you should pass the id of the row which needs the button to be disabled. You get the row index in ngFor:
<div *ngFor="let n of records; let i = index">
<span>{{n.name}}</span>
<span>{{n.location}}</span>
<button (click)="addtomainrecord(i)" [disabled]="disablebutton[i]">add</button>
</div>
And the the method will catch that index to set the button state:
addtomainrecord(index) {
this.disablebutton[index] = true;
}
Demo
I hope it will work
<div *ngFor="let n of records">
<span>{{n.name}}</span>
<span>{{n.location}}</span>
<button (click)="addtomainrecord(n)" [disabled]="n.disablebutton">add</button>
</div>
addtomainrecord(record) {,
record.disablebutton=true;
//rest of the code follows
}
For reference: Disable button with ngFor
本文标签: javascriptAngular 2 disabling button of a particular rowStack Overflow
版权声明:本文标题:javascript - Angular 2+ disabling button of a particular row - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744250900a2597243.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论