ionic-framework - 单击特定的 ion-item 按钮会触发其他按钮
问题描述
假设数组中有 3 个元素names
。因此,下面的 html 将显示 3 个项目,每个项目都附加了单击按钮。
.html
<ion-item *ngFor="let name of names">
<p>{{name.username}}</p>
<button ion-button [someAttribute]="isLoad" (click)="clickTest(name.id)">
Click
</button>
</ion-item>
.ts
async clickTest(id) {
isLoad = true;
// await server side call
isLoad = false;
}
要求:我只希望特定按钮更改为isLoad = true
,而不是所有按钮。
问题:单击Click
按钮时,其他按钮也会更改。
解决方案
这与 Javascript 如何处理事件(事件冒泡)有关。
简而言之,您可以这样做:
像这样传递事件并调用 stopPropagation 方法。
在.html中
<ion-item *ngFor="let name of names">
<p>{{name.username}}</p>
<button ion-button [someAttribute]="isLoad" (click)="clickTest($event, name.id)">
Click
</button>
</ion-item>
.ts
async clickTest(event:any, id) {
event.stopPropagation();
isLoad = true;
// await server side call
isLoad = false;
}
或者,在模板中调用这样的方法:
在.html中
<ion-item *ngFor="let name of names">
<p>{{name.username}}</p>
<button ion-button [someAttribute]="isLoad" (click)="clickTest(name.id), $event.stopPropagation()">
Click
</button>
</ion-item>
推荐阅读
- python - session.query 返回不在数据库中的对象 - sqlalchemy
- vba - 使用活动工作表中的单元格选择将组合框填充到用户窗体中
- javascript - 通过小部件将 ISO-Date 替换为 HTML 文档中的普通日期
- javascript - 如何使用 onclientclick 调用多个 Javascript 函数
- python - 如何将 numpy 的混合元素类型附加到另一个数组?
- c# - Kubernetes 中的 Azure AD 身份验证无法取消对 message.State 的保护
- javascript - 如何通过反应导航访问 redux 商店?
- excel - 如何根据 Excel 中的整数输入自动创建列表
- python - 在后台生成进程
- reactjs - 电子生产构建总是有框架 - macos