Integration of Angular and JQuery.iCheck by using a directive is not working

前端 未结 3 1980
被撕碎了的回忆
被撕碎了的回忆 2020-12-30 02:41

I\'m trying to integrate JQuery.iCheck (plugin for checkbox&Radio buttons styling). I followed few suggestions here that said that the way to integrate a jQ

相关标签:
3条回答
  • 2020-12-30 03:22

    With the code from XaReSx answer I had one issue : When updating the model from the scope the radio doesn't changed. I had to add a timeout in the model watch :

    $scope.$watch($attrs['ngModel'], function (newValue) {
        $timeout(function () {
            $(element).iCheck('update');
        })
    })
    
    0 讨论(0)
  • 2020-12-30 03:35

    Here is a better sololution by hackthis02

      .directive('icheck', function ($timeout, $parse) {
        return {
            require: 'ngModel',
            link: function ($scope, element, $attrs, ngModel) {
                return $timeout(function () {
                    var value;
                    value = $attrs['value'];
    
                    $scope.$watch($attrs['ngModel'], function (newValue) {
                        $(element).iCheck('update');
                    });
    
                    $scope.$watch($attrs['ngDisabled'], function (newValue) {
                        $(element).iCheck(newValue ? 'disable' : 'enable');
                        $(element).iCheck('update');
                    })
    
                    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('ifClicked', function (event) {
                        if ($(element).attr('type') === 'radio' && $attrs['ngModel']) {
                            return $scope.$apply(function () {
                                //set up for radio buttons to be de-selectable
                                if (ngModel.$viewValue != value)
                                    return ngModel.$setViewValue(value);
                                else
                                    ngModel.$setViewValue(null);
                                ngModel.$render();
                                return
                            });
                        }
                    });
                });
            }
        };
    });
    
    0 讨论(0)
  • 2020-12-30 03:36

    Already solved on this link: https://github.com/fronteed/iCheck/issues/62 by wajatimur

    webApp.directive('icheck', function($timeout, $parse) {
        return {
            require: 'ngModel',
            link: function($scope, element, $attrs, ngModel) {
                return $timeout(function() {
                    var value;
                    value = $attrs['value'];
    
                    $scope.$watch($attrs['ngModel'], function(newValue){
                        $(element).iCheck('update');
                    })
    
                    return $(element).iCheck({
                        checkboxClass: 'icheckbox_flat-aero',
                        radioClass: 'iradio_flat-aero'
    
                    }).on('ifChanged', function(event) {
                        if ($(element).attr('type') === 'checkbox' && $attrs['ngModel']) {
                            $scope.$apply(function() {
                                return ngModel.$setViewValue(event.target.checked);
                            });
                        }
                        if ($(element).attr('type') === 'radio' && $attrs['ngModel']) {
                            return $scope.$apply(function() {
                                return ngModel.$setViewValue(value);
                            });
                        }
                    });
                });
            }
        };
    });
    
    0 讨论(0)
提交回复
热议问题