import {Component, forwardRef, Input, OnInit} from '@angular/core'; import {ControlValueAccessor, NG_VALUE_ACCESSOR} from '@angular/forms'; @Component({ selector: 'customSwitch', templateUrl: './template.html', styleUrls: ['./style.less'], providers: [{ provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => CustomSwitch), multi: true }] }) export default class CustomSwitch implements ControlValueAccessor, OnInit { @Input() disabled; @Input() on; @Input() off; checked = false; private onChange; ngOnInit(): void { } writeValue(obj: any): void { this.checked = obj; } registerOnChange(fn: any): void { this.onChange = fn; } registerOnTouched(fn: any): void { } setDisabledState(isDisabled: boolean): void { } onModelChange(value) { this.checked = value; this.onChange && this.onChange(value); } }