javascript - Angular Directive 未能添加元素图标

标签 javascript angularjs angularjs-directive

我尝试使用 Angular 指令动态制作按钮。

这是我当前的代码:

function rippleButton() {
    return {
        restrict: 'E',
        compile: function (element, attributes) {
            element.addClass('btn waves-effect waves-light');
            if (attributes.color) {
                element.addClass('btn-' + attributes.color);
            }
            if (attributes.size) {
                element.addClass('btn-' + attributes.size);
            }
            if (attributes.type == "Submit") {
                element.addClass('btn-primary');
            }
            if (attributes.icon) {
                element.append("<i class=fa " + attributes.icon + "></i>");
            }
        }
    };
}

和我的html: <ripple-button type="button" data-color="danger" icon="fa-search">Filter</ripple-button>

除了图标之外,一切都很完美。当它呈现到浏览器时,它变成:

<ripple-button type="button" data-color="danger" icon="fa-search" class="btn waves-effect waves-light btn-danger">Filter<i class="fa" fa-search=""></i></ripple-button>

fa-search不与 <i class=""> 结合。

我应该怎么做才能让它发挥作用?

谢谢

最佳答案

我已经测试了下面的代码,它可以工作:

element.append("<i class='fa " + attributes.icon + "'></i>");

关于javascript - Angular Directive 未能添加元素图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34921941/

相关文章:

javascript - 鼠标相对于 div 的位置

javascript - 谷歌地图中两点之间的多条折线

javascript - 禁用 DIV 的 ajax 加载内容上的单击事件

javascript - $compile 显示未捕获的 TypeError : undefined is not a function

javascript - 如何从列表项文本中删除日期

AngularJS 拦截并扩展 Controller $scope

html - AngularJS:使用 ng-repeat 动态加载 CSS 样式表

javascript - 为什么 angular.isNumber() 没有按预期工作?

javascript - AngularJS 指令不包含在父元素中

css - Angular JS : Can we assign custom CSS to an HTML element more than once in a directive?