<mat-autocomplete #auto="matAutocomplete">
<mat-option *ngFor="let person of filteredPersons | async" [value]="person.name" (onSelectionChange)="selectedPersonsInDialog(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(person)">
{{person}}
</mat-option>
</mat-autocomplete>
这是我在组件类中的方法: - 实际上 selectedPersonsInDialog函数被调用两次,总是如此 显示2次对话框,其中最新选中,前一个选中 值。
I want to display a dialog only once with latest value selected.
selectedPersonsInDialog(person){
this.selectedPerson=person;
alert(this.selectedPerson);
let dialogRef = this.dialog.open(AddListOfPersonDialog, {
width: '500px',
data: { person:this.selectedPerson}
});
}*
答案 0 :(得分:0)
当选择更改事件在每个选项中传递时,所以获得了多个事件。但是在您选择的一个事件中$event.source.selected
为true
。所以你可以管理它。
你的HTML应该像
<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>
你的组件功能应该是
selectedPersonsInDialog(isSelected: boolean,person){
if(isSelected){
this.selectedPerson=person;
alert(this.selectedPerson);
let dialogRef = this.dialog.open(AddListOfPersonDialog, {
width: '500px',
data: { person:this.selectedPerson}
});
}
}