Стоит ли указывать в Angular2 директиву об изменении выпадающего списка для вызова сервиса? - PullRequest
0 голосов
/ 30 сентября 2019

Мой вопрос: стоит ли реализовывать директиву 'change' для выпадающего списка, чтобы обновить другой компонент?

с простым заменой:

<select formControlName="group" class="form-control">
    <option (change)="this.update(item.id)" *ngFor="let item of this.cities" 
        [value]="item.id" >
            {{item.name}}
    </option>
  </select>

или реализовать ее с помощью директивы:

@Directive({
  selector: '[appDropdownDirective]'
})
export class DropdownDirectiveDirective {
  private el: HTMLInputElement;

  constructor(private elementRef: ElementRef, weatherService: WeatherService) {
    this.el = this.elementRef.nativeElement;
  }

  @HostListener('change', ['$event.target.value'])
  onFocus(value: any) {
    this.weatherService.update(this.el.value);
  }
}
...