首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Angular ng-bind-html解包锚标签

Angular ng-bind-html解包锚标签
EN

Stack Overflow用户
提问于 2014-04-04 08:48:51
回答 1查看 1.2K关注 0票数 1

我在下面的代码中使用ng-bind-html:

代码语言:javascript
复制
<a href="/test">
    <article>
        <p>
            Some content goes here
        </p>
    </article>
</a>

我这样做是为了让整个内容区域成为一个大锚点。但是,当使用ng-bind-html时,我得到以下输出:

代码语言:javascript
复制
<!-- my anchor tag is closed and stripped! -->
<a></a>
<p>
     Some content goes here
</p>

在使用$sce.trustAsHtml显式转义输出时:

代码语言:javascript
复制
<!-- anchor tag closed -->
<a href="/test"></a>
<article>
    <!-- random anchor added to the top of every nested element -->
    <a href="/test"></a>

    <p>
        Some content goes here
    </p>
</article>
EN

回答 1

Stack Overflow用户

发布于 2014-05-09 14:38:11

我通过创建一个自定义指令来解决这个问题,这个指令的作用就像一个锚。当它被添加到周围的div中时,它不会引起上面的问题。

代码语言:javascript
复制
exports.directive('anchor', [function () {

        return {
            restrict: 'AE',
            link: function (scope, element, attributes) {
                element.addClass('anchor');
                element.on('click', function () {
                    window.location.href = attributes.anchor;
                });
            }
        };

    }]);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22851595

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档