angular ng-bind-html and directive within it

前端 未结 6 1683
北荒
北荒 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:08

    I was also facing this problem and after hours searching the internet I read @Chandermani's comment, which proved to be the solution. You need to call a 'compile' directive with this pattern:

    HTML:

    JS:

    .directive('compile', ['$compile', function ($compile) {
        return function(scope, element, attrs) {
            scope.$watch(
                function(scope) {
                    // watch the 'compile' expression for changes
                    return scope.$eval(attrs.compile);
                },
                function(value) {
                    // when the 'compile' expression changes
                    // assign it into the current DOM
                    element.html(value);
    
                    // compile the new DOM and link it to the current
                    // scope.
                    // NOTE: we only compile .childNodes so that
                    // we don't get into infinite loop compiling ourselves
                    $compile(element.contents())(scope);
                }
            );
        };
    }])
    

    You can see a working fiddle of it here

提交回复
热议问题