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

前端 未结 3 1989
被撕碎了的回忆
被撕碎了的回忆 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: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);
                            });
                        }
                    });
                });
            }
        };
    });
    

提交回复
热议问题