javascript - Angular JS : Directive to change all links within a block of html

标签 javascript angularjs angularjs-directive

使用 Angular Directive(指令)有没有办法将 target="_blank"添加到所有 <a>我从第三方 CMS 中提取的一大块 HTML 中的标签?

类似于:

<div ng-bind-html="post.body" updatelinks></div>

最佳答案

最简单的指令如下所示:

app.directive('updatelinks', function($timeout) {
    return {
        link: function(scope, element) {
            $timeout(function() {
                element.find('a').prop('target', '_blank');
            });
        }
    };
});

关于javascript - Angular JS : Directive to change all links within a block of html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28384127/

相关文章:

javascript - AngularJS =?绑定(bind)不更新

javascript - 如何在 WordPress 中实现 javascript

javascript - Google Apps 脚本 - 如何中断循环以防止出现错误消息

javascript - Angular : redirecting/#%21/to/#!/

javascript - AngularUI Accordion 切换范围变量

javascript - Angular Directives 如何使用重复数据

javascript - createElement 和伪类

javascript - d3.js 链接强度对力图中的链接距离的影响

javascript - Angular promise 返回 "undefined"值 .NET MVC

jquery - 重新编译 Angular 元素,但不重新绑定(bind) Angular 事件