【问题标题】:Angular 9 Add click event to dynamic buttonsAngular 9将点击事件添加到动态按钮
【发布时间】:2020-07-16 15:01:03
【问题描述】:

我有一些需要为工具栏添加的按钮,但我似乎无法正确地向它们添加点击事件,否则会感觉像是 hack。

这是我设置/返回按钮的方式:

    export class ImportButtons {
      getButtons(): ButtonItem[] {
         return [new ButtonItem({
                 group: 'imports',
                 section: 'admin',
                 name: 'Approve',
                 class: 'btn btn-primary approve-all-button',
                 text: 'Approve',
                 hasMenu: true,
                 menuClass: 'approve-all-menu',
                 subItems: [new SubItem({ value: 'Approve All' })]
              }),
              new ButtonItem({
              group: 'imports',
              section: 'admin',
              name: 'OnHold',
              class: 'btn btn-primary on-hold-button',
              text: 'On Hold',
              hasMenu: true,
              menuClass: 'on-hold-menu',
              subItems: [new SubItem({ value: 'On Hold All' }),
              new SubItem({ value: 'Revert from On Hold' }),
              new SubItem({ value: 'Revert All from On Hold' })]
             })
           ];
      }
    }

这是在我的 html 中抓取并显示它们:

    <agc class="btn-group dropdown drop-button-container" ngbdropdown="" placement="bottom-right" ng-reflect-placement="bottom-right">
        <ng-container *ngFor="let buttons of this.fillTable('import', 'admin')">
           <ng-container *ngIf="buttons.hasMenu === true">
               <button class="{{buttons.class}}" type="button" (click)="buttonClicked( buttons );" > {{buttons.text}} </button>
               <button aria-haspopup="true" class="btn btn-primary dropdown-toggle dropdown-toggle-split admin-split-button icon-fa-caret-down" data-toggle="dropdown" ngbdropdowntoggle="" type="button" aria-expanded="false"></button>
               <ng-container *ngFor="let sub of buttons.subItems">
                 <agc class="dropdown-menu" ngbdropdownmenu="">
                 <span class="dropdown-item c-pointer" (click)="buttonClicked( buttons, sub )" >{{sub.value}}</span>
                 </agc>
               </ng-container>
           </ng-container>
        </ng-container>
      </agc>

我目前尝试在单击时调用一个方法 ('buttonClicked()'),它会告诉我单击了什么。但是当我单击一个按钮时,我看到其余按钮闪烁,就好像它正在执行 PostBack。正因为如此,它感觉就像一个 hack。

有正确的方法吗?添加的每个按钮都会在单击时(显然)有自己的调用方法,我找不到在单击事件中使用字符串的方法。

例如,我最初在按钮类中有一个“click”属性,我将在其中放置要调用的方法名称 - 所以“click = 'onHoldClick($event)'”和 html 点击看起来像“(click) ='{{buttons.click}}'" 但它不喜欢那样。

html 遍历它从方法“fillTable”获取的按钮,它看起来像这样:

    fillTable(groupName: string, section: string): ButtonItem[] {           
       switch (this.page.currentPage) {
         case 'imports':
           return this.importButtons.getButtons().filter(n => n.section === section);
         case 'export':
           return this.exportButtons.getButtons().filter(n => n.section === section);
         case 'sales':
           return this.salesButtons.getButtons().filter(n => n.section === section);
         case 'bom':
           return this.bomButtons.getButtons().filter(n => n.section === section);
       }
     }

【问题讨论】:

  • 嗨!你试过 (click)="buttonClicked($event) 吗?
  • 嗨@yanesof__ 我确实尝试过,但什么也没做。不过还是谢谢。

标签: angular angular-components dynamic-html


【解决方案1】:

我认为问题出在使用函数时的 *ngFor 中。我建议使用辅助变量

this.buttonsActual=this.fillTable('import', 'admin');

//and make the *ngFor using 
<ng-container *ngFor="let buttons of buttonsActual">

你也可以试试trackBy

顺便说一句,你的想法是正确的(click)="buttonClicked(button)"。好吧,您只能传递 button.name 或其他任何内容。那么你有两种方法,在函数中使用大开关

buttonClicked(button)
{
   switch (button.name)
   {
      case "...":
      break;
      case "...":
      break;
      ...
   }
}

或者将不同的函数存储在一个对象中

command={
  one:this.function1
  two:this.function2
}
function1(){..}
function2(){..}
//and in buttonClicked
buttonClicked(button)
{
   command[button.name]();
}

【讨论】:

  • 感谢您的回复。我目前正在用一个开关来处理它以确定点击了什么。如果这是一种合理的做法,我会保留它并继续这样做。我确实喜欢您将其存储在对象中的想法,这是一种很酷的方式。我会再坚持一段时间,看看还有谁发表意见。
猜你喜欢
  • 1970-01-01
  • 2018-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多