首页 > 解决方案 > 为什么我的 (selectionChange) 不断执行?

问题描述

我使用自定义多选自动完成创建了一个角度项目,该项目具有

  @Output() selectionChange: EventEmitter<any[]> = new EventEmitter<any[]>();

以及下面的方法

onSelectionChange(val : any) {
const filteredValues = this.getFilteredOptionsValues();
let count = 0;
if (this.multiple) {
  this.selectedValue?.filter(item => {
      if (filteredValues.includes(item)) {
          count++;
      }
  });
  this.selectAllChecked = count === this.filteredOptions.length;
}
this.selectedValue = val.value;
this.selectionChange.emit(this.selectedValue);
}

我在我的其他组件中使用它

            <div style="width: 100%; margin-right: 26px; margin-bottom: 15px;">
          <multi-select-autocomplete class="input-medium"
                                     [placeholder]="'Search and Select Brands'"
                                     [options]="companies"
                                     [display]="'name'"
                                     [value]="'id'"
                                     [labelCount]="10"
                                     [label]="'Brands'"
                                     (selectionChange)="selectBrand($event)">
          </multi-select-autocomplete>
        </div>
      </div>

但是我遇到了一个问题,即 (selectionChange) 方法在我进行选择之前就一直在触发。

我可能出错的任何想法?

标签: htmlangulartypescript

解决方案


推荐阅读