如何访问 *ngFor 循环中的一个按钮并在单击事件上为其分配公共删除方法
我的循环可以跨越按钮并分配属性。
.html
<ng-container matColumnDef="actions">
<mat-header-cell mat-header-cell *matHeaderCellDef>{{
'scenarios.scenariosList.actionsColumn' | translate
}}</mat-header-cell>
<mat-cell class="mr-3" mat-cell *matCellDef="let element">
<div class="w-100 d-flex justify-content-start align-items-center" *ngFor="let button of actionButtons">
<button mat-icon-button matTooltip="{{ button.tooltipMsg | translate }}" color="{{ button.color }}">
<mat-icon>{{ button.icon }} </mat-icon>
</button>
</div>
</mat-cell>
</ng-container>
.ts 循环中的值
public confirmDialog(scenarioId: string): void {
const dialogRef = this.dialog.open(ScenarioListDialogComponent, {
maxWidth: '400px',
data: { scenarioId }
});
dialogRef
.afterClosed()
.subscribe((dialogResult) => {
this.result = dialogResult;
})
.unsubscribe();
}
interface ActionButton {
icon: string;
tooltipMsg: string;
color?: string;
}
public actionButtons: ActionButton[] = [
{
icon: 'edit',
tooltipMsg: 'scenarios.scenariosList.editBtnTooltipMsg',
color: 'primary'
},
{
icon: 'cloud_upload',
tooltipMsg: 'scenarios.scenariosList.uploadBtnTooltipMsg',
color: 'accent'
},
{
icon: 'delete',
tooltipMsg: 'scenarios.scenariosList.deleteBtnTooltipMsg',
color: 'warn'
}
];
我还拥有如下所示的方法确认方法,三个按钮包含我试图完成的 ,就是将方法confirence> confircondialog
分配给具有iCON的按钮:'DELETE:' '
并在单击时调用该方法。不幸的是,我最终将此方法分配给所有三个按钮,我似乎无法理解NG循环如何工作以及如何解决此问题。
有帮助吗?
我在NGFOR内尝试了NGIF,但出现错误...
I have loop that goes over buttons and assigns properties.
.html
<ng-container matColumnDef="actions">
<mat-header-cell mat-header-cell *matHeaderCellDef>{{
'scenarios.scenariosList.actionsColumn' | translate
}}</mat-header-cell>
<mat-cell class="mr-3" mat-cell *matCellDef="let element">
<div class="w-100 d-flex justify-content-start align-items-center" *ngFor="let button of actionButtons">
<button mat-icon-button matTooltip="{{ button.tooltipMsg | translate }}" color="{{ button.color }}">
<mat-icon>{{ button.icon }} </mat-icon>
</button>
</div>
</mat-cell>
</ng-container>
.ts
I also have the method confirmDialog as seen below and three buttons that contain values from the loop above
public confirmDialog(scenarioId: string): void {
const dialogRef = this.dialog.open(ScenarioListDialogComponent, {
maxWidth: '400px',
data: { scenarioId }
});
dialogRef
.afterClosed()
.subscribe((dialogResult) => {
this.result = dialogResult;
})
.unsubscribe();
}
interface ActionButton {
icon: string;
tooltipMsg: string;
color?: string;
}
public actionButtons: ActionButton[] = [
{
icon: 'edit',
tooltipMsg: 'scenarios.scenariosList.editBtnTooltipMsg',
color: 'primary'
},
{
icon: 'cloud_upload',
tooltipMsg: 'scenarios.scenariosList.uploadBtnTooltipMsg',
color: 'accent'
},
{
icon: 'delete',
tooltipMsg: 'scenarios.scenariosList.deleteBtnTooltipMsg',
color: 'warn'
}
];
What I am trying to accomplish is to assign the method confirmDialog
to the button that has icon: 'delete'
and call on that method on click. Unfortunately I am ending up assigning this method to all three buttons and I cannot seem to comprehend how ng loops work and how can I solve this.
Any help?
I tried ngIf inside ngFor but error comes up...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否还希望渲染其他两个按钮?我要假设是的。因此,我们将所有按钮渲染:
但是仅在按钮上,其中
button.icon
是'delete'
我们可以调用enckerndialog()
在其他上将无需做任何事情''
。Do you still want the other 2 buttons to be rendered? I'm going to assume yes. So we render all buttons with:
But only on button, where
button.icon
is'delete'
we can callconfirmDialog()
on others it will do nothing''
.这不是你想要的吗?
我刚刚将有条件的表达式用作点击处理程序。
我个人将EG通过按钮模型到通用
ActionPerformed(B:ActionButton)
,然后从组件代码中进行剩下的Isnt this what you want?
I have just used conditional expression as click handler.
Personally I would eg pass button model to a generic
actionPerformed(b:ActionButton)
and do the rest from the component code有多种方法可以处理这个问题,在我看来,我有几种:
方法 1
在 .ts 文件中,您将拥有辅助方法 方法
2
如果您知道删除索引将始终位于最后,那么在 .html 中,您可以通过将
let last = last
添加到 ngFor 来跟踪最后一个元素,然后绑定confirmDialog以单击There are various ways to deal with this, on top of my mind I have a few:
Approach 1
In .ts file you would have the helper method
Approach 2
If you know that delete index would be last always then in your .html you can track the last element by adding
let last = last
to the ngFor and then bind confirmDialog to click