简体   繁体   中英

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

Using an angular directive is there a way to add target="_blank" to all <a> tags within a large block of HTML that i am pulling from a third party CMS?

Something like:

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

The simplest directive would look like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM