angular ng-bind-html and directive within it

前端 未结 6 1676
北荒
北荒 2020-11-22 11:28

Plunker Link

I have a element which I would like to bind html to it.

That works.

6条回答
  •  清酒与你
    2020-11-22 12:10

    Add this directive angular-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) {
            // Incase 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);
          });
        }
      };
    }]);
    

    Use it like this :

    Really easy :)

提交回复
热议问题