html - 如果按钮来自Angular中的列表,那么识别按下按钮的最佳方法是什么?
问题描述
我有一个项目列表,我正在使用 *ngFor 显示这些项目。而且我在每个迭代的独立项目上都有一个带有功能的按钮。代码如下。
<ul *ngFor="let data of datas">
// . . .
<button
type="button"
class="btn btn-secondary btn-circle btn-sm"
(click)="showDetail(**parameter**)"
> show
</button>
// . . .
这是我的表演功能。我不知道在这个参数中传递什么。
increment(**parameter**) {
console.log(this.data[**parameter**]['price']);
}
我想显示特定选定项目的价格。我的数据列表如下。
[
{ id: "1111", guest: "abc", price: 20 },
{ id: "222", guest: "xyz", price: 30 } ]
解决方案
您可以data
作为参数发送
<ul *ngFor="let data of datas">
...
<button type="button" class="btn btn-secondary btn-circle btn-sm" (click)="showDetail(data)">
show
</button>
...
</ul>
increment(data) {
console.log(data.price);
}
或者你可以发送index
<ul *ngFor="let data of datas; let i = index">
...
<button type="button" class="btn btn-secondary btn-circle btn-sm" (click)="showDetail(i)">
show
</button>
...
</ul>
increment(index) {
console.log(this.data[index]['price']);
}
推荐阅读
- reactjs - React Redux 更新嵌套对象的干净方式?
- node.js - NodeJS12 Morgan logger跳过不使用旋转文件流
- html5-video - 与 WCAG 1.2.5 音频描述(预录)相关的没有音频的通用生活方式背景视频的可访问性规则
- scala - 如何更改火花中的日志级别?
- ruby-on-rails - 禁用到 Rails 控制台的日志输出
- r - 如何过滤包含向量中部分但不是全部元素的数据框中的向量
- swift - 为什么在 Swift 中向下转换数组项?
- javascript - 快速鼠标移动不会触发 CSS 悬停
- python - VS Code,如何为python指定启动文件
- c++ - Visual Studio 中的 wxwidgets 应用程序给出错误“LNK2019 无法解析的外部符号”