javascript - 如何使用 angularjs 中的自定义指令通过 Unform jQuery 插件设置浏览器默认复选框的样式

标签 javascript jquery angularjs angularjs-directive jquery-uniform

我只是想设置我正在使用的复选框的样式 http://uniformjs.com带有 angularjs 的 jquery 插件。

这是我想使用插件设置样式的复选框输入元素:

<label><input type="checkbox" plugin-uniform id="inline">&nbsp;Inline editing</label>

这是我为此编写的自定义指令:

    myApp.directive('pluginUniform', function() {
        return {
            restrict: 'A',
            link: function(scope, element, attrs) {
                Layout.initUniform();
            }
        };
    });

这是Layout.initUnform()代码:

    var Layout = function() {

    //other stuff

            initUniform: function (els) {
                if (els) {
                    jQuery(els).each(function () {
                            if ($(this).parents(".checker").size() == 0) {
                                $(this).show();
                                $(this).uniform();
                            }
                        });
                } else {
                    handleUniform();
                }
            }

    function handleUniform() {
        if (!jQuery().uniform) {
            return;
        }
        var test = $("input[type=checkbox]:not(.toggle), input[type=radio]:not(.toggle, .star)");
        if (test.size() > 0) {
            test.each(function () {
                    if ($(this).parents(".checker").size() == 0) {
                        $(this).show();
                        $(this).uniform();
                    }
                });
        }
    }

    //other stuff

    }

我已经准备好了 uniform.default.cssjquery.uniform.js

有人可以告诉我如何将浏览器默认复选框样式设置为 http://uniformjs.com使用自定义指令在 angularjs 中使用 jQuery 插件复选框样式?

最佳答案

调用 Layout.initUniform() 时缺少 els 参数

由于当 jQuery 库包含在页面中的 Angular 库之前时,指令的链接函数已经将元素公开为 jQuery 对象,因此您实际上不需要布局函数

您的 handleUnform 函数有几个缺陷。

要检查插件是否存在,应该执行if($.fn.pluginName)。 另外,如果您有许多元素与此指令绑定(bind),那么每次指令运行时您都会循环遍历所有元素。

试试这个:

myApp.directive('pluginUniform', function() {
    return {
        restrict: 'A',
        link: function(scope, element, attrs) {
           /* element is jQuery object*/
           if (!element.parents(".checker").length) { /* size() is deprecated */
                 element.show().uniform();                          
           }
        }
    };
});

关于javascript - 如何使用 angularjs 中的自定义指令通过 Unform jQuery 插件设置浏览器默认复选框的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25827201/

相关文章:

javascript - pdf渲染时显示加载图像

javascript - AngularJS select,设置下拉菜单项的背景色

javascript - D3.js 强制布局突出显示,无需单击或悬停

javascript - Three.js顶点着色器: update attributes and how to save a value for the next processing round

javascript - 如何在另一个组件中访问一个组件的常量变量 - React

如果 *any* 属性匹配,JQuery 选择元素

javascript - 使用短\长文本垂直对齐按钮中的文本

javascript - 单击 HTML 按钮后禁用该按钮的最快方法是什么?

javascript - AngularJS:如何保持在 'run' 方法直到完成

javascript - 查找然后执行任务的功能