ng-bind-html中 自定义的指令 不生效!

问题:使用ng-bind-html 页面上已经生成了正确的html代码,但是标签中的 指令 不生效!
js代码:

clipboard.png

html代码:

clipboard.png

阅读 3.1k
1 个回答

当然无法生效,ng-bind-html 等同于 innerHTML

可以自定义一个类似 ng-bind-html-compile 的指令:

.directive('bindHtmlCompile', ['$compile', function ($compile) {
        return {
            restrict: 'A',
            link: function (scope, element, attrs) {
                scope.$watch(function () {
                    return scope.$eval(attrs.bindHtmlCompile);
                }, function (value) {
                    // In case value is a TrustedValueHolderType, sometimes it
                    // needs to be explicitly called into a string in order to
                    // get the HTML string.
                    element.html(value && value.toString());
                    // If scope is provided use it, otherwise use parent scope
                    var compileScope = scope;
                    if (attrs.bindHtmlScope) {
                        compileScope = scope.$eval(attrs.bindHtmlScope);
                    }
                    $compile(element.contents())(compileScope);
                });
            }
        };
    }]);
<div ng-bind-html-compile="getId(xxx)"></div>
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题