最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Rendering html in Angular with Directive - Stack Overflow

programmeradmin6浏览0评论

I am storing html data in my database from the output of a WYSIWYG editor. The html also stores the html for some directives. One of them being the ui-bootstrap tooltip:

<span tooltip-placement="left" tooltip="On the Left!">Tooltip text here</span>

I am able to get any other html elements to work by using the binding:

<div ng-bind-html-unsafe="html.content"></div>

But the html with the directive's reference doesn't interact with the actual directive.

How can I get the directive to work?

Do I have to pile the html or something like that?

I am storing html data in my database from the output of a WYSIWYG editor. The html also stores the html for some directives. One of them being the ui-bootstrap tooltip:

<span tooltip-placement="left" tooltip="On the Left!">Tooltip text here</span>

I am able to get any other html elements to work by using the binding:

<div ng-bind-html-unsafe="html.content"></div>

But the html with the directive's reference doesn't interact with the actual directive.

How can I get the directive to work?

Do I have to pile the html or something like that?

Share Improve this question asked Sep 12, 2013 at 3:49 SneakstaSneaksta 1,0614 gold badges26 silver badges48 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

Yes, since the markup you are trying to render contains directives, you need to pile it so they are processed by Angular.

You could create a directive that does that for you:

app.directive('htmlRender', function($pile) {
  return {
    restrict: 'E',
    scope: { html: '@' },
    link: function(scope, element) {
      scope.$watch('html', function(value) {
        if (!value) return;

        var markup = $pile(value)(scope);
        element.append(markup);
      });
    }
  };
});

Plunker here.

Do I have to pile the html or something like that?

Yes You need to plile the HTML as Angular will only consider as below as simple html

<div ng-bind-html-unsafe="html.content"></div>

Check below is documentation for $pile

$plie documenation

发布评论

评论列表(0)

  1. 暂无评论