0

I am trying to use Material Design Lite in Angular 2 and have trouble updating checkboxes after the state has changed. See the following Plunker example.

When the user clicks on "All" to select all boxes, only the normal checkboxes update. I have tried using componentHandler.upgradeDom() and componentHandler.upgradeAllRegistered() but it made no difference.

How can I get the data-binding to work?

2 Answers 2

2

Ok, so I had a similar problem like you, and after good 2 or 3 hours of Googling around, I came up with a solution (or maybe it's just a dirty hack, don't know):

<label class="mdl-checkbox mdl-js-checkbox mdl-js-ripple-effect" [class.is-checked]="isChecked()">
    <input type="checkbox" [(ngModel)]="checkbox.Checked" class="mdl-checkbox__input">
    <span class="mdl-checkbox__label">A label</span>
</label>

I've updated the plunk you've shared so you can see it there. I hope this solves your problem, as it have solved mine.

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

Comments

0

After reading the MDL code, it seemed to me that the appearance of the MDL checkbox only changes when it sees the onchange event. But just setting "checked" in code doesn't deliver the changed event. So, after I set it, I give it a poke.

function setChecked(element, bool) {
    element.checked = bool;
    var evt = document.createEvent("HTMLEvents");
    evt.initEvent("change", false, true);
    element.dispatchEvent(evt);
}

Now, I'm not saying this is the RIGHT or BEST way to do this, but it works for me.

1 Comment

Is there a good way to get the references of my checkbox elements?

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.