我正在使用 Angular v6.0.0,我的组件是实现 ControlValueAccessor;
在更改事件中,我看到它总是旧值,但是当绑定 ngModel 的值时,它是新值。我做了一个stackblitz 例子;
@HostListener('click')
onToggle() {
if (this.disabled) {
return;
}
this.checked = !this.checked;
this.change.emit(this.checked);
this.changed(this.checked);
this.touched(this.checked);
}
// Implement control value accessor
changed = (_: any) => {};
touched = (_: any) => {};
public writeValue(obj: any) {
if (obj !== this.checked) {
this.checked = !!obj;
}
}
public registerOnChange(fn: any) {
this.changed = fn;
}
public registerOnTouched(fn: any) {
this.touched = fn;
}