角度ng-bind-html和其中的指令 [英] angular ng-bind-html and directive within it

查看:42
本文介绍了角度ng-bind-html和其中的指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Plunker链接

我有一个元素想要将html绑定到它.

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

<div ng-bind-html="details" upper></div>

那行得通.现在,与此同时,我还有一个绑定到绑定的html的指令:

That works. Now, along with it I also have a directive which is bound to the bound html:

$scope.details = 'Success! <a href="#/details/12" upper>details</a>'

但是带有div和anchor的指令upper不会求值.我该如何运作?

But the directive upper with the div and anchor do not evaluate. How do I make it work?

推荐答案

我也遇到了这个问题,经过数小时的互联网搜索,我读到了@Chandermani的评论,事实证明这是解决方案. 您需要使用以下模式调用"compile"指令:

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:

<div compile="details"></div>

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

这篇关于角度ng-bind-html和其中的指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆