javascript - AngularJS : Scope value changing, but doesn't always apply ng-class -
i'm working on creating 'pretty' checkboxes , radio boxes in angular. have similar plugins developed in jquery, i'm having trouble angular versions, checkbox.
what i've done create directive (prettycheckbox) , template div wrapping checkbox input:
<div class="prettycheckbox" ng-click="togglecb($event)" ng-class="{ 'checked': checkbox }"> <input id="{{inputid}}" type="checkbox" ng-model="checkbox"> </div>
as can see, model value checked, changes div class checked, when not, not. input given display: none;
, looks good. when designed it, set work label's attribute, , ok.
then remembered can wrap input label, , clicking label should trigger input. tested code on it, , became super buggy. saw scope watch , console logs had setup see variable triggered 3 times every 1 click on div-checkbox, , twice when clicking labels, , 1 of each of these times wouldn't change value. made no sense. i've between tweaking bit now, , have no idea what's real issue, nor how solve it. i'm hoping can advice. i've created plunker code: http://plnkr.co/edit/ovzsuhqnldpvtuirh2sm?p=preview. note plunker make bunch of console logs.
one of annoying inconsistencies when see variable change ng-class doesn't trigger , change accordingly (when clicking on label).
edit: got box changing via apply, still no luck multiple clicks.
edit 2: tried modify more answers double click, ended tweaking it, , cleaned up. kind of why clicking checkbox in second scenario (wrapped label) ended clicking 3 times: clicking clicked wrapping label, that's still 2 clicks. donno #3 comes from, solution seems work. here's new plunkr: http://plnkr.co/edit/vvidshshvsq1akvfpduc?p=preview
as updating scope variable click event dom, need tell angular update binding via running digest cycle. in case manipulating scope binding on label.click
event, inside need run digest cycle using scope.$apply()
code
label.click(function() { scope.togglecb(); scope.$apply() });
Comments
Post a Comment