如何访问 *ngFor 循环中的一个按钮并在单击事件上为其分配公共删除方法

发布于 2025-01-17 19:08:56 字数 1877 浏览 5 评论 0原文

我的循环可以跨越按钮并分配属性。

.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

内心激荡 2025-01-24 19:08:56

您是否还希望渲染其他两个按钮?我要假设是的。因此,我们将所有按钮渲染:

   (click)="button.icon === 'delete' ? confirmDialog(element.scenarioId) : ''"

但是仅在按钮上,其中button.icon'delete'我们可以调用enckerndialog()在其他上将无需做任何事情''

<button
   (click)="button.icon === 'delete' ? confirmDialog(element.scenarioId) : ''"
   mat-icon-button 
   matTooltip="{{ button.tooltipMsg | translate }}" 
   color="{{ button.color }}">
   <mat-icon>{{ button.icon }} </mat-icon>
</button>

Do you still want the other 2 buttons to be rendered? I'm going to assume yes. So we render all buttons with:

   (click)="button.icon === 'delete' ? confirmDialog(element.scenarioId) : ''"

But only on button, where button.icon is 'delete' we can call confirmDialog() on others it will do nothing ''.

<button
   (click)="button.icon === 'delete' ? confirmDialog(element.scenarioId) : ''"
   mat-icon-button 
   matTooltip="{{ button.tooltipMsg | translate }}" 
   color="{{ button.color }}">
   <mat-icon>{{ button.icon }} </mat-icon>
</button>
记忆里有你的影子 2025-01-24 19:08:56

这不是你想要的吗?

        <div class="w-100 d-flex justify-content-start align-items-center" *ngFor="let button of actionButtons">
          <button mat-icon-button (click)="button.icon=='delete' ? doIt():null">
            <mat-icon>{{ button.icon }} </mat-icon>
          </button>
        </div>

我刚刚将有条件的表达式用作点击处理程序。

我个人将EG通过按钮模型到通用ActionPerformed(B:ActionButton),然后从组件代码中进行剩下的

        <div class="w-100 d-flex justify-content-start align-items-center" *ngFor="let button of actionButtons">
          <button mat-icon-button (click)="buttonAction(button)">
            <mat-icon>{{ button.icon }} </mat-icon>
          </button>
        </div>


buttonAction(button:ActionButton){
   if(button.icon='delete') {doIt(); return}
}

Isnt this what you want?

        <div class="w-100 d-flex justify-content-start align-items-center" *ngFor="let button of actionButtons">
          <button mat-icon-button (click)="button.icon=='delete' ? doIt():null">
            <mat-icon>{{ button.icon }} </mat-icon>
          </button>
        </div>

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

        <div class="w-100 d-flex justify-content-start align-items-center" *ngFor="let button of actionButtons">
          <button mat-icon-button (click)="buttonAction(button)">
            <mat-icon>{{ button.icon }} </mat-icon>
          </button>
        </div>


buttonAction(button:ActionButton){
   if(button.icon='delete') {doIt(); return}
}
高跟鞋的旋律 2025-01-24 19:08:56

有多种方法可以处理这个问题,在我看来,我有几种:

方法 1

  • 将辅助方法添加到现有按钮
<mat-icon (click)="helper(button.icon)">{{ button.icon }} </mat-icon>

在 .ts 文件中,您将拥有辅助方法 方法

helper(icon: string):void {
 if (icon === 'delete') {
    this.confirmDialog()
 }
}

2

如果您知道删除索引将始终位于最后,那么在 .html 中,您可以通过将 let last = last 添加到 ngFor 来跟踪最后一个元素,然后绑定confirmDialog以单击

<div 
class="w-100 d-flex justify-content-start align-items-center" 
*ngFor="let button of actionButtons; let last = last">
    <button mat-icon-button 
    matTooltip="{{ button.tooltipMsg | translate }}" 
    color="{{ button.color }}">
        <mat-icon>{{ button.icon }} </mat-icon>
        <mat-icon ngIf="last" (click)="confirmDialog()">{{ button.icon }} </mat-icon>
    </button>
</div>

There are various ways to deal with this, on top of my mind I have a few:

Approach 1

  • Add helper method to existing button
<mat-icon (click)="helper(button.icon)">{{ button.icon }} </mat-icon>

In .ts file you would have the helper method

helper(icon: string):void {
 if (icon === 'delete') {
    this.confirmDialog()
 }
}

Approach 2

If you know that delete index would be last always then in your .html you can track the last element by addinglet last = last to the ngFor and then bind confirmDialog to click

<div 
class="w-100 d-flex justify-content-start align-items-center" 
*ngFor="let button of actionButtons; let last = last">
    <button mat-icon-button 
    matTooltip="{{ button.tooltipMsg | translate }}" 
    color="{{ button.color }}">
        <mat-icon>{{ button.icon }} </mat-icon>
        <mat-icon ngIf="last" (click)="confirmDialog()">{{ button.icon }} </mat-icon>
    </button>
</div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文