Из документации https://v6.angular.io/api/forms/SelectControlValueAccessor
SelectControlValueAccessor - это директива, а не интерфейс
Класс:
import { Component, OnInit, Output, EventEmitter, Renderer2, ElementRef } from '@angular/core';
import { NG_VALUE_ACCESSOR, SelectControlValueAccessor } from '@angular/forms';
@Component({
selector: 'select-input',
template: ` `,
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => SelectInputComponent),
multi: true
}
]
})
export class SelectInputComponent extends SelectControlValueAccessor {
value: any;
onChange: (_: any) => void;
onTouched: () => void;
compareWith: (o1: any, o2: any) => boolean;
compareFn(c1: any, c2: any): boolean {
return c1 && c2 ? c1.key === c2.key : c1 === c2;
}
writeValue(value: any): void {
if (value) {
this.value.emit(value); }
}
registerOnChange(fn: (value: any) => any): void {
this.onChange = fn;
}
registerOnTouched(fn: () => any): void {
this.onTouched = fn;
}
setDisabledState(isDisabled: boolean): void {
throw new Error('Method not implemented.');
}
constructor(_renderer: Renderer2, _elementRef: ElementRef) {
super(_renderer, _elementRef);
}
}
HTML - выберите:
<select class="form-control" [compareWith]="compareFn" [(ngModel)]="value">
<option value="">Choose a option</option>
<option *ngFor="let option of options" [ngValue]="option">{{option.name}}</option>
</select>