Angular2 - Material2 - Custom checkbox

I am working on creating a custom checkbox based on angular material2 project. Everything seems to work at first, but when I update the model values ​​in code, the checkbox does not cancel the check, even though angular registers the change. See plunker for a demo.

Relevant code to update model values:

private _parentValue:bool = false;
get parentValue()
{
    return this._parentValue;
}
set parentValue(val)
{
    this._parentValue = coerceBooleanProperty(val);

    this.value1 = this._parentValue;
    this.value2 = this._parentValue;
    this.value3 = this._parentValue;
}

      

Apologies in advance if I did anything wrong here as this is my first post on Stack Overflow.

+3


source to share


1 answer


I would rewrite the method writeValue

like this:

writeValue(value: T) {
    if (isDefined(value)) {
        this._value = value;
    }
}

      

where isDefined

is a function such as



export function isDefined(val: any): boolean {
  return val !== null && val !== undefined;
}

      

This way your control can consume the false

value

Forked plunker

+2


source







All Articles