2

nativeelement.value在我的项目中,由于一些只读错误,必须使用 ElementRef 。只有我的指令

export class DeliveryAcrossDirective {
  @Input('key') key: string;
  @Input('component') component: string;
  constructor(
    private store: Store,
    private elementRef: ElementRef<HTMLInputElement>
  ) {
    this.key = '';
    this.component = '';
  }
  @HostListener('change') onChange() {
    console.log('noticed something');

    this.store.dispatch<IAction<IAdjust>>({
        type: RDX_DELIVERY_ACROSS_ADJUST,
        payload: {
          key: this.key,
          value: this.elementRef.nativeElement.value
        },
        component: this.component
      })
  }

}

没有从我的垫子选择中捕获更改事件

<mat-form-field class="full-width" [@transformRightLeftStateTrigger]="stateDown | async">
  <mat-label>
    {{ country | async }}
  </mat-label>
  <mat-select [formControl]="countryFormControl"
  appDeliveryAcross
  [key]="'iso'"
  [component]="'delivery-across'" >
    <mat-option *ngFor="let language of (languages | async)"  [value]="language.value">
      {{ language.country }}
    </mat-option>
  </mat-select>
</mat-form-field>

而经典输入确实

        <mat-form-field class="full-width" [@transformRightLeftStateTrigger]="stateDown | async">
          <input matInput
          [formControl]="minFormControl"
          [errorStateMatcher]="errorStateMatcher"
          placeholder="Minimaal"
          appDeliveryAcross
          [key]="'min'"
          [component]="'delivery-across'"
          type="number">
        </mat-form-field>

有谁知道如何使用指令从 mat select 中捕获更改事件?

4

2 回答 2

0

我不确定您使用的是哪个版本的 Angular Material,但对于https://stackoverflow.com/a/50223943/9602452change可能不存在简单事件mat-select

好像您已经修改了mat-selects的指令

于 2021-09-04T20:29:06.827 回答
0

要检测mat-select您不需要的更改HostListener,只需订阅selectionChangeMatSelect

export class DeliveryAcrossDirective implements OnInit, OnDestroy {
  selectionSub: Subscription;

  constructor(
    private host: MatSelect
  ) {
  }

  ngOnInit(): void {
    this.selectionSub = this.host.selectionChange.subscribe(option => {
      console.log(option);
      // Do something
    });
  }

  ngOnDestroy(): void {
    this.selectionSub?.unsubscribe();
  }
}
于 2021-09-04T20:50:15.847 回答