Compiling dynamic HTML strings from database

前端 未结 5 1295
花落未央
花落未央 2020-11-22 02:14

The Situation

Nested within our Angular app is a directive called Page, backed by a controller, which contains a div with an ng-bind-html-unsafe attribute. This is

5条回答
  •  终归单人心
    2020-11-22 03:12

    Try this below code for binding html through attr

    .directive('dynamic', function ($compile) {
        return {
          restrict: 'A',
          replace: true,
          scope: { dynamic: '=dynamic'},
          link: function postLink(scope, element, attrs) {
            scope.$watch( 'attrs.dynamic' , function(html){
              element.html(scope.dynamic);
              $compile(element.contents())(scope);
            });
          }
        };
      });
    

    Try this element.html(scope.dynamic); than element.html(attr.dynamic);

提交回复
热议问题