Checkbox and toggle-switches; No model updates

364 views
Skip to first unread message

Olivier Clément

unread,
Apr 15, 2013, 5:16:28 PM4/15/13
to ang...@googlegroups.com
Ok, I'm on my way to a solution but hitting a lots of bumps on the road;
If you have something better than what's below, please let me know!

Here's the Gist (or skip to the plunk !)

I have a model that contains a boolean, among other things; Naturally, I want to use a checkbox to allow a user to change its value;
But I'm not satisfied with the look of the checkbox so I'd like to use a library like this one: https://github.com/tdreyno/iphone-style-checkboxes

I see is that these libraries need to programatically check or uncheck the checkboxes they drive; Either by setting the "checked" attribute or by simulating a click through $(...).trigger('click'), or whatever that works. The problem I see is that doing so won't update the model at all;

Considering I don't really want to rewrite the whole plugin as a standalone directive (why reinvent the wheel?), how can I address this issue?

Here's a plunk to get started: http://plnkr.co/edit/YAy6eU4W2dOaRySeg0Rn

Thanks

Sebastian Sebald

unread,
Apr 15, 2013, 6:09:54 PM4/15/13
to ang...@googlegroups.com
The easiest way would be to bind the check box to the change event and then fire the $apply, I guess. See the docs: http://docs.angularjs.org/api/ng.$rootScope.Scope

Olivier Clément

unread,
Apr 16, 2013, 11:16:07 AM4/16/13
to ang...@googlegroups.com
Yeah I was pretty "sure" I had to use $apply, but didn't quite realize how to do that without having to modify the plugin/library itself, or "what" to apply exactly

While writing my first reply (which I didn't post in the end) I realized it was as simple as reading the checked property and assigning this to model in the scope...
Simple enough; Not too sure how I didn't figured this out sooner...! :P 

Thanks for your answer

Dave

unread,
Apr 16, 2013, 6:30:06 PM4/16/13
to ang...@googlegroups.com
Olivier,

Would you mind updating plnkr with your changes? Would love to use the same one :)

Thank you
Reply all
Reply to author
Forward
0 new messages