简体   繁体   English

mat-select(Angular Material):当用户选择默认值时,selectionChange 不会触发

[英]mat-select (Angular Material): selectionChange not firing when user selects the default value

(may be a duplicate, but couldn't find a question with this specific issue) (可能是重复的,但找不到与此特定问题有关的问题)

Using Angular 13 with Angular-Material 13.3.9.使用 Angular 13 和 Angular-Material 13.3.9。 I implemented a simple mat-select for selecting filters (all, monthly, yearly and creator).我实现了一个简单的 mat-select 来选择过滤器(全部、每月、每年和创建者)。 The mat-select provides a preselected value, which is monthly. mat-select 提供一个预选值,即每月。 Whenever the user changes the mat-selects value, an event is fired by the selectionChange EventEmitter, which works fine so far - except for one single issue:每当用户更改 mat-selects 值时,selectionChange EventEmitter 就会触发一个事件,到目前为止它工作正常 - 除了一个问题:

Whenever the user selects a different value, eg yearly, and then selectes the preselected value again (monthly), the selectionChange event is not fired.每当用户选择不同的值(例如每年),然后再次选择预选值(每月)时,不会触发 selectionChange 事件。

Why?为什么? And how can i get the event to fire in this specific case?在这种特定情况下,我怎样才能让事件触发?

Already tested:已经测试过:

  • Tried to use other preselected values, eg yearly.尝试使用其他预选值,例如每年。 Issue is the same as with monthly, but of course now the event is not fired when the user selects yearly again.问题与每月相同,但是当用户再次选择每年时,当然不会触发该事件。
  • Hardcoding the mat-options did not change the behaviour.硬编码 mat-options 并没有改变行为。
  • Removed translation-pipe just to be sure it doesn't interfere did not change the behaviour.删除翻译管道只是为了确保它不会干扰并没有改变行为。

.html: .html:

<mat-form-field appearance="fill">
  <mat-label>{{ 'filterprojects.showas' | translate }}</mat-label>
  <mat-select [(value)]="selectedMainFilter.value" (selectionChange)="onMainFilterChange()">
    <mat-option [value]="option.value" *ngFor="let option of mainFilters">{{ 'filterprojects.' + option.viewValue | translate }}</mat-option>
  </mat-select>
</mat-form-field>

.ts .ts

@Component({
  selector: 'app-filterprojects',
  templateUrl: './filterprojects.component.html',
  styleUrls: ['./filterprojects.component.css']
})
export class FilterprojectsComponent implements OnInit {

  //Filter-Values
  mainFilters: MainFilterElement[] = [
    { value: 'all', viewValue: 'filterall' },
    { value: 'yearly', viewValue: 'filteryearly' },
    { value: 'monthly', viewValue: 'filtermonthly' },
    { value: 'creator', viewValue: 'filtercreator' }
  ];

  selectedMainFilter: MainFilterElement = this.mainFilters[2]; //Select the Monthly-Filter by default
  
  public onMainFilterChange() {
    console.log("Hi! I will not be fired, if the monthly-mainFilter is selected by the user.");
  }
}

Assign selectedMainFilter value inside component rather template在组件而不是模板内分配selectedMainFilter

selectedMainFilter: string = this.mainFilters[2].value;

and use in template like并在模板中使用

<mat-select [(value)]="selectedMainFilter" ...

now you can get selected value现在您可以获得选定的值

onMainFilterChange() {
 console.log(this.selectedMainFilter)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM