【问题标题】:using angularjs directive for icheck plugin对 icheck 插件使用 angularjs 指令
【发布时间】:2014-01-21 00:03:51
【问题描述】:

我的 Angularjs 应用程序出现问题,无法弄清楚问题出在哪里。

在 plunker 上查看Here

当我点击按钮时,它应该运行 iCheck() 插件,但它没有

【问题讨论】:

    标签: angularjs angularjs-directive icheck


    【解决方案1】:

    可以在此处找到更好的 iCheck 指令:https://github.com/fronteed/iCheck/issues/62 (wajatimur)

    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);
                        });
                    }
                });
            });
        }
    };
    

    【讨论】:

      【解决方案2】:

      您应该做一件事以使一切正常运行:强制 AngularJS 实际使用 jQuery

      AngularJS 在其源代码中包含 jQlite,但它与 jQuery 不同。如果你包含 jQuery before angular.js,那么 Angular 将使用它而不是自己的实现。

      因此只需在 index.html 中更改:

      <script data-require="jquery@*" data-semver="2.0.3" src="http://code.jquery.com/jquery-2.0.3.min.js"></script>
      <script data-require="angular.js@*" data-semver="1.2.9" src="http://code.angularjs.org/1.2.9/angular.js"></script>
      

      (jQuery 在 angular.js 之前)

      working Plunker

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-02-16
        • 2019-04-24
        • 1970-01-01
        • 1970-01-01
        • 2015-02-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多