Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular two-way-binding with sub elements

my problem is with creating an angular directive.

I want to create a group of checkboxes with just one single ng-model. It's like a bitfield or flags, i.e. the checkboxes have values from 0, 1, 2, 3 up to n but for ng-model I just want to add 2^value of all checked checkboxes. The values to add are then 1, 2, 4, 8, 16, ...

I wonder if there is a better, more correct or just simpler solution to my problem.

http://plnkr.co/edit/9h7EkEpDohXTniIDHdc5

In the example you can change the value in the textbox and the checks will be updated, but not the other way. It's a bit crazy, the code is working on my dev machine, but not in Plnkr!

app.directive('ngCheckboxFlags', function () {
return {
    restrict: 'A',
    require: 'ngModel',
    link: function (scope, element, attrs, ctrls) {
        var flagSum;
        var checkboxes = [];

        // trigger changes of the ngModel
        scope.$watch(attrs.ngModel, function (value) {
            flagSum = value;
            for (var i = 0, ii = checkboxes.length; i < ii; ++i) {
                var checkbox = checkboxes[i];
                checkbox.checked = flagSum & (1<<checkbox.value);
            }
        });

        for (var i = 0, inputs = element.find('input[type=checkbox]'), ii = inputs.length; i < ii; ++i) 
        {
            var checkbox = inputs[i];
            checkboxes.push(checkbox);
            // trigger changes of HTML elements
            $(checkbox).bind('change', function () {
                flagSum = ctrls.$viewValue ^ (1<<this.value);
                console.log(flagSum);

                //ERROR: Change not happening, textbox shows old value
                scope.$apply(function () {
                    ctrls.$setViewValue(flagSum);
                });
            });
        }
    }
};
});

thnx in advance knut

like image 719
kwrl Avatar asked Jan 29 '26 19:01

kwrl


1 Answers

Don't bind the ng-model directly to primitive types, use an object instead:

$scope.sum = {
  ofCheckValues: 3
};

And on the HTML:

ng-model="sum.ofCheckValues"

“Whenever you have ng-model there’s gotta be a dot in there somewhere. If you don’t have a dot, you’re doing it wrong.”

plunker: http://plnkr.co/edit/NRAk3kyP1rdDmYrsz6ZL?p=preview

like image 57
bmleite Avatar answered Feb 01 '26 14:02

bmleite



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!