From b645bfd1b7cada1104b67b07dcb2304d9fcee23b Mon Sep 17 00:00:00 2001 From: "Manu Mtz.-Almeida" Date: Mon, 19 Nov 2018 18:46:46 +0100 Subject: [PATCH] fix(angular): fix controlValueAccessor for boolean fixes #16371 --- .../control-value-accessors/boolean-value-accessor.ts | 7 ++++++- .../directives/control-value-accessors/value-accessor.ts | 6 +++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/angular/src/directives/control-value-accessors/boolean-value-accessor.ts b/angular/src/directives/control-value-accessors/boolean-value-accessor.ts index 8bc3f35801b..613ff08f341 100644 --- a/angular/src/directives/control-value-accessors/boolean-value-accessor.ts +++ b/angular/src/directives/control-value-accessors/boolean-value-accessor.ts @@ -1,7 +1,7 @@ import { Directive, ElementRef, HostListener } from '@angular/core'; import { NG_VALUE_ACCESSOR } from '@angular/forms'; -import { ValueAccessor } from './value-accessor'; +import { ValueAccessor, setIonicClasses } from './value-accessor'; @Directive({ /* tslint:disable-next-line:directive-selector */ @@ -20,6 +20,11 @@ export class BooleanValueAccessor extends ValueAccessor { super(el); } + writeValue(value: any) { + this.el.nativeElement.checked = this.lastValue = value == null ? '' : value; + setIonicClasses(this.el); + } + @HostListener('ionChange', ['$event.target.checked']) _handleIonChange(value: any) { this.handleChangeEvent(value); diff --git a/angular/src/directives/control-value-accessors/value-accessor.ts b/angular/src/directives/control-value-accessors/value-accessor.ts index ca1108a4a23..fa4ff2f78ad 100644 --- a/angular/src/directives/control-value-accessors/value-accessor.ts +++ b/angular/src/directives/control-value-accessors/value-accessor.ts @@ -5,9 +5,9 @@ export class ValueAccessor implements ControlValueAccessor { private onChange: (value: any) => void = () => {/**/}; private onTouched: () => void = () => {/**/}; - private lastValue: any; + protected lastValue: any; - constructor(private el: ElementRef) {} + constructor(protected el: ElementRef) {} writeValue(value: any) { this.el.nativeElement.value = this.lastValue = value == null ? '' : value; @@ -41,7 +41,7 @@ export class ValueAccessor implements ControlValueAccessor { } } -function setIonicClasses(element: ElementRef) { +export function setIonicClasses(element: ElementRef) { requestAnimationFrame(() => { const classList = (element.nativeElement as HTMLElement).classList;