gpt4 book ai didi

Angular 4 : Disable button in ngFor after click

转载 作者:行者123 更新时间:2023-12-04 01:21:18 25 4
gpt4 key购买 nike

我有一个 <button>在 ngFor 循环中,我希望在用户单击按钮后将其禁用。循环的每个元素都有一个按钮,因此我必须为每个元素使用不同的 bool 值来区分它们。

这是来自 html 的代码片段:

<div class="card" *ngFor="let i of items">
<button type="button" [disabled]="'btn' + i.id" (click)="btn + i.id=true">TEST</button>
<div>
[disabled]="'btn' + i.id"部分似乎有效,但我无法将其值设置为 true使用 (click)="btn + i.id=true" .如何连接 btni.id并将其值设置为true?

任何帮助表示赞赏!

最佳答案

来自 head 的代码(可能有错误):

在您的 .ts 组件中使用数组:

buttons = Array(10).fill(false); // e.g. 10 = size of items

在您的模板中:
<div class="card" *ngFor="let i of items; index as j">
<button type="button" [disabled]="buttons[j]" (click)="buttons[j]=true">TEST</button>
<div>
index as j适用于 Angular 5/6,低版本使用 let j=index
替代方案

添加到禁用的项目字段并直接使用该字段:
<button type="button" [disabled]="item.disabled" (click)="item.disabled=true">TEST</button>

关于 Angular 4 : Disable button in ngFor after click,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53276941/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com