How to prevent Dialog opening twice in angular material using angular5

前端 未结 1 1552
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-22 17:43
    
 

        
相关标签:
1条回答
  • 2021-01-22 18:12

    when selection change event pass in every option, so got more then one event. but you got $event.source.selected is true in one event which is you selected. so you can manage it.

    your html should be like

    <mat-autocomplete #auto="matAutocomplete">
                    <mat-option *ngFor="let person of filteredPersons | async" [value]="person.name"
                                (onSelectionChange)="selectedPersonsInDialog($event.source.selected,person)">
                        <img style="vertical-align:middle;" aria-hidden  src="{{person.imgUrl}}" width="30" height="30"/>
                        <span>{{ person.name }}</span>
                        <small>ID: {{person.id}}</small>
                    </mat-option>
                </mat-autocomplete>
                <mat-autocomplete #auto="matAutocomplete">
                <mat-option *ngFor="let person of filteredPersons |  async" [value]="person.name"
                            (onSelectionChange)="selectedPersonsInDialog($event.source.selected,person)">
                    {{person}}
                 </mat-option>
            </mat-autocomplete>
    

    your component function should be like

    selectedPersonsInDialog(isSelected: boolean,person){
    if(isSelected){
       this.selectedPerson=person;
       alert(this.selectedPerson); 
       let dialogRef = this.dialog.open(AddListOfPersonDialog, {
          width: '500px',
          data: { person:this.selectedPerson}
        });
      }
    }
    
    0 讨论(0)
提交回复
热议问题