首页 文章

Angular Material Expansion面板单击事件

提问于
浏览
0
<mat-expansion-panel (opened)="panelOpenState = true" (closed)="panelOpenState = false" (click)="getPrimaryAddress()">
  <mat-expansion-panel-header>
    <mat-panel-title>
      Primay Address
    </mat-panel-title>
    <mat-panel-description>
      {{panelOpenState ? 'Hide' : 'Display'}} Address
    </mat-panel-description>
  </mat-expansion-panel-header>
  <div>
    <mat-form-field class="example-form-field" style="margin-top: 20px;">
      <input matInput type="text" placeholder="Street 1" [(ngModel)]="streetOneValue">
      <button mat-button *ngIf="streetOneValue " matSuffix mat-icon-button aria-label="Clear" (click)="streetOneValue=''">
                  <mat-icon>close</mat-icon>
                </button>
    </mat-form-field>

    <mat-form-field class="example-form-field" style="margin-top: 20px;">
      <input matInput type="text" placeholder="Street 2" [(ngModel)]="streetTwoValue">
      <button mat-button *ngIf="streetTwoValue " matSuffix mat-icon-button aria-label="Clear" (click)="streetTwoValue=''">
                  <mat-icon>close</mat-icon>
                </button>
    </mat-form-field>
  </div>
  <div>
    <mat-form-field class="example-form-field" style="margin-top: 20px;">
      <input matInput type="text" placeholder="Street 3" [(ngModel)]="streetThreeValue">
      <button mat-button *ngIf="streetThreeValue " matSuffix mat-icon-button aria-label="Clear" (click)="streetThreeValue=''">
                  <mat-icon>close</mat-icon>
                </button>
    </mat-form-field>
    <mat-form-field class="example-form-field" style="margin-top: 20px;">
      <input matInput type="text" placeholder="Street 2" [(ngModel)]="countyValue">
      <button mat-button *ngIf="countyValue " matSuffix mat-icon-button aria-label="Clear" (click)="countyValue=''">
                  <mat-icon>close</mat-icon>
                </button>
    </mat-form-field>
  </div>
  <div>
    <mat-form-field class="example-form-field" style="margin-top: 20px;">
      <input matInput type="text" placeholder="Post Code" [(ngModel)]="postcodeValue">
      <button mat-button *ngIf="postcodeValue " matSuffix mat-icon-button aria-label="Clear" (click)="postcodeValue=''">
                  <mat-icon>close</mat-icon>
                </button>
    </mat-form-field>

    <mat-form-field class="example-form-field" style="margin-top: 20px;">
      <input matInput type="text" placeholder="Country" [(ngModel)]="primaryAddresscountryValue">
      <button mat-button *ngIf="primaryAddresscountryValue " matSuffix mat-icon-button aria-label="Clear" (click)="primaryAddresscountryValue=''">
                  <mat-icon>close</mat-icon>
                </button>
    </mat-form-field>
  </div>
</mat-expansion-panel>

刚刚开始使用Angular Material并遇到了mat-expansion-panel问题 . 我的面板在展开时会显示一系列mat-form-field元素 .

我有几个点击事件,一个获取数据; (click)=“getPrimaryAddress(),其余的只是在选择X按钮后清除数据,例如(click)=”streetOneValue =''“

但是,当我单击X按钮清除特定值时,getPrimaryAddress()事件再次触发,只是重新填充元素中的数据 . 无论如何,只要我选择其他点击事件,就会停止触发getPrimaryAddress()函数?

我需要数据延迟加载,这就是我在点击事件而不是OnInit处理它的原因

1 回答

  • 1

    您可以在已经使用的 opened 事件中进行加载 . 然后,您不需要使用click事件,它也更正确 . 根据我的理解,您希望在面板扩展时加载数据 . 您还可以使用另一个 afterExpand 事件 .

    按下按钮时,面板上会出现 click 事件,因为按钮是面板的子元素,并且您也在面板元素中处理 click ,因此两者都接收事件 . 此外,面板内的任何鼠标点击都会触发另一个我认为不是您想要的加载 . 这就是为什么您最好使用材质组件提供的特定事件 .

相关问题