【问题标题】:How do I assign different IDs to automatically generated buttons? (Angular)如何为自动生成的按钮分配不同的 ID? (角度)
【发布时间】:2022-01-18 10:04:20
【问题描述】:

下面的源代码显示了 IONIC 页面的一部分。这个页面是关于形状和形状的信息。对于每个形状,页面上显示 2 个按钮:形状属性按钮和材料信息按钮。我的问题是我是否可以为按钮提供不同的 ID?

例如:我有一个包含 2 个形状的页面。 Shape1 有两个按钮 shape-properties-button 和 material-information-button。 和 Shape2 具有相同的 2 个按钮。 我希望 Shape1 和 Shape2 的按钮 material-information-button 表现不同。

<ion-row class="datatable-content" *ngFor="let shape of shapeGroup.shapes; index as j">
        <ion-row class="content-row">
          <ion-col size="2">
            {{shape.externalid}}
          </ion-col>
          <ion-col size="6">
            {{shape.shapeName}}
          </ion-col>
          <ion-col size="4">
            {{shape.shapeType}}
          </ion-col>
        </ion-row>
        <ion-row class="options-header">
          <ion-col size="6" size-xs="6" size-sm="6" size-md="6" size-lg="6" size-xl="6">
            <ion-buttons>
              <ion-button id="shape-properties-button" class="table-button-detailview"
                (click)="selectShapeProperty(j)">
                Details
              </ion-button>
            </ion-buttons>
          </ion-col>
          <ion-col size="6" size-xs="6" size-sm="6" size-md="6" size-lg="6" size-xl="6">
            <ion-buttons>  
              <ion-button id="material-information-button" class="table-button-detailview" 
                (click)="selectBridgeInformation(j)">
                Brückeninformationen
              </ion-button>
            </ion-buttons>
          </ion-col>         
        </ion-row>
        <ion-row *ngIf="shouldShapePropertyBeShown(j)" size="12" class="content-subrow" id="shape-properties" >
          <ion-col size="4" *ngFor="let property of shape.shapeProperties" style="margin-top: 8px; background-color:#f9efef; ">
           <div class="detailView__label">{{property.propertyName}}</div> 
           <div class="detailView__text"> {{property.propertyValue}}</div>
          </ion-col>
        </ion-row>
        <ion-row *ngIf="shouldBridgeBeShown(j)" size="12" class="content-subrow" id="shape-properties" >
          <ion-col size="12" size-xs="12" size-sm="12" size-md="12" size-lg="12" size-xl="12">
              <ion-row>

【问题讨论】:

    标签: javascript html angular typescript ionic-framework


    【解决方案1】:

    只需在这两个按钮中传递shape 作为第二个参数

    <ion-button id="shape-properties-button" class="table-button-detailview"
           (click)="selectShapeProperty(j,shape)">
               Details
    </ion-button>
    

    并在方法中检查形状类型并相应地执行操作。

    selectShapeProperty(index,shapeObj){
     switch (shapeObj.shapeType){
       cases 'Circle':
         do something;
       case ....
          ....
       default:
         console.error('unknown shape')
     }
    
    }
    
    

    注意:在您提供的代码中,我认为在 ngFor 中创建 id 没有意义,除非它们是唯一的。


    更新:

    试试

     <ion-button [id]="'shape-properties-button-'+(j+1)" class="table-button-detailview"
                    (click)="selectShapeProperty(j)">
                    Details
                  </ion-button>
    

    【讨论】:

    • 首先,感谢您的快速回复。 ngFor 在源代码的第一行。但是第二个参数如何帮助我更改自动生成按钮的 ID?
    • 我希望 Shape1 的按钮具有 ID shape-properties-button1 和 Shape2 shape-properties-button2 等等。
    • @Doncarlito87:检查更新的答案
    • 这似乎有效。我还可以创建一个根据按钮数量创建不同布尔变量的函数吗?
    • @Doncarlito87 请将其标记为答案并投票,以便其他寻找类似内容的人有所帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-20
    • 2018-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-19
    • 1970-01-01
    相关资源
    最近更新 更多