3

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

The relevant code to update the model values is:

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;
}

I apologize in advance if I have done something incorrect here as this is my first time posting to stack overflow.

1 Answer 1

2

I would rewrite writeValue method as follows:

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

where isDefined is a function like

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

This way your control can consume false value

Forked Plunker

Sign up to request clarification or add additional context in comments.

1 Comment

Wow! That was it. I was under the impression that the 'writeValue' method was only used for setting the initial value of the control. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.