Pallab Mandal
Pallab Mandal

Reputation: 137

Use Linky _blank in angular $filter

I am using linky to create the link from text with a valid URL. Using ng-bind-html="string | linky:'_blank'" was breaking the HTML rendering. So I created a custom filter, so that issue is solved. No, i have the Link and the proper body. But now the URL is opening in the same Tab. How do I apply the _blank attribute to linky?

My code:

    <span ng-bind-html="to_trusted(body | linkyWithHtml)"></span>


    app.filter('linkyWithHtml', function($filter) {
  return function(value) {
    if(value){
      var linked = $filter('linky')(value.toString());
      var replaced = linked.replace(/\&gt;/g, '>').replace(/\&lt;/g, '<');
      return replaced;
    }
  };
});

Upvotes: 2

Views: 865

Answers (2)

Antoine
Antoine

Reputation: 4029

The correct solution is to use the "target" argument of the linky filter: https://docs.angularjs.org/api/ngSanitize/filter/linky#overview

app.filter('linkyWithHtml', function($filter) {
  return function(value) {
    if(value){
      var linked = $filter('linky')(value.toString(), '_blank');
      return linked;
    } else {
      return value;
    }
  };
});

Upvotes: 0

Pallab Mandal
Pallab Mandal

Reputation: 137

So finally I got the solution 


app.filter('linkyWithHtml', function($filter) {
  return function(value) {
    if(value){
      var linked = $filter('linky')(value.toString());
      var replaced = linked.replace(/\&gt;/g, '>').replace(/\&lt;/g, '<').replace("<a", "<a target='_blank'")
      return replaced;
    } else{
      return value;
    }
  };
});

Thanks to https://stackoverflow.com/users/5924562/naren-murali

Upvotes: 1

Related Questions