vendredi 3 novembre 2017

Icheck doesn't capture the first switch from true to false

Hi' I'm using Icheck with angularjs and I have a problem with the first switch from true to false, the $watch on ngModel isn't called and so it keeps value true in my angular variable. Where is the error? This is the code

app.directive('icheck2', function ($timeout, $parse) {
    return {
        require: 'ngModel',
        link: function ($scope, element, $attrs, ngModel) {
            return $timeout(function () {
                $scope.$watch($attrs['ngModel'], function (newValue) {
                    $(element).iCheck('update');
                }); 

                    //Initialize first checkbox
                    if ($(element)[0].value == "true")
                        $(element).iCheck('check');
                    else
                        $(element).iCheck('update');//even uncheck doesn't work


                return $(element).iCheck({
                    checkboxClass: 'icheckbox_square-blue',
                    radioClass: 'iradio_square-blue'
                }).on('ifChanged', function (event) {
                    if ($(element).attr('type') === 'checkbox' && $attrs['ngModel']) {
                        $scope.$apply(function () {
                            return ngModel.$setViewValue(event.target.checked);
                        })
                    }
                });
            });
        }
    };
});

On HTML I have:

<div data-ng-repeat="accessory in reservation.accessories"
    style="margin-Top: 10px;"
    data-ng-if="accessory.idType == accessoryType.CHECKBOX_TYPE">
    <input icheck2 type="checkbox" name="iCheck2" data-ng-model="accessory.value"
        value="" id="checkbox">
    <span style="margin-left: 5px;"></span>
</div>




Aucun commentaire:

Enregistrer un commentaire