javascript - HTML 未在 Angular Directive(指令)内编译

标签 javascript angularjs angularjs-directive

angular.module("vtApp.directives").
directive('multirangeslider', function ($compile) {
    return {
        restrict: 'E',
        replace: true,
        link: function (scope, element, attrs) {
            var variances, values, options, template, compile;
            scope.variances = eval (attrs.variances);
            scope.values = scope.$eval(attrs.values);
            var htmlText = '<div id="'+attrs.id+'"><table class="CRC"><tbody><tr>';
            for (var i=0; i<scope.values.length; i++) {
                htmlText += '<td id="val' + i + '" style="width:' + scope.values[i] + '%;"> ' + scope.variances[i] + ': <strong>{{ranges[' + i + ']}}%</strong></td>';
            }
            htmlText += '</tr></tbody></table></div>';

            template = angular.element(htmlText);
            $compile(template)(scope);
            element.replaceWith(htmlText);

        }
    }
});

在我的 View 中:

<multirangeslider values="ranges" variances="['Master', 'Template A', 'Template B']"></multirangeslider>

html 未编译。这是一个 JsFiddle

最佳答案

template 是您编译的元素。 htmlText 是原始的 html 字符串。将 element.replaceWith(htmlText); 更改为 element.replaceWith(template);

关于javascript - HTML 未在 Angular Directive(指令)内编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16382206/

相关文章:

javascript - 在 IE 9 中设置对象 "Object doesn' t 支持此操作”

JavaScript Date.parse 不接受阿拉斯加时区?

javascript - 检查一组单选按钮是否至少选择了一个

javascript - ng-model 对象到字符串

javascript - Angular Firebase 不反射(reflect)全日历 View 上的变化

javascript - 在 $http.get 从 angularjs 中的 RESTful API 获取数据时放置一个微调器

javascript - Nodejs 项目无法在 heroku 中启动

javascript - 如何以 Angular 在 View 中显示指令范围的对象

json - 加载 Angular UI Bootstrap Datepicker 时将日期字符串解析为 Date 对象

AngularJS $compile 模板到 iframe 并打印它