Create components using ngModel

@ Angular / forms, there are interfaces used to implement support [(ngModel)], the specific can be found ControlValueAccessor, yet in-depth understanding here just for the development of components and a preliminary understanding

interface ControlValueAccessor {
  writeValue(obj: any): void
  registerOnChange(fn: any): void
  registerOnTouched(fn: any): void
  ...
}

We need to do is implement this interface

import {NG_VALUE_ACCESSOR,ControlValueAccessor} from "@angular/forms"
@Component({
  selector: 'app-search-select',
  templateUrl: './search-select.component.html',
  styleUrls: ['./search-select.component.less'],
  providers: [{
    provide: NG_VALUE_ACCESSOR,
    useExisting: forwardRef(() => SearchSelectComponent),
    multi: true
  }]
})
  export class SearchSelectComponent implements ControlValueAccessor {
      writeValue(obj: any): void
      registerOnChange(fn: any): void
      registerOnTouched(fn: any): void
  }
 

 

  

Guess you like

Origin www.cnblogs.com/llcMite/p/11449516.html