9

I am working with rss feed and got rss feed from my server so i am sending stringfy data but when i bind this in my angular 2 application it shows text with html tags so how we can remove this tags. I just want to show text only.

Here is server code:

exports.newspage=function(req,resp){
db.executeSql("select header,text,teaser from news_d",function(data,err){
        if(err){
            resp.writeHead(200,{"Content-Type":"text/html"});
            resp.write("<html><head><title>500</title></head></html>");
        }
        else{
            resp.writeHead(200,{"Content-Type":"x-application/json"});
            resp.write(JSON.stringify(data));
        }
        resp.end();
    });
};

app.get('/newspage', function(req, resp) {

    emp.newspage(req,resp);

});

service.ts:

gettagesnews(){
     let headers = new Headers();
       headers.append('x-access-token',this.token);
         var options = new RequestOptions({headers: headers});
      return this.http.get('http://services.com:9000/newspage/',options).map((res:Response) => res.json())
    .catch((error:any) => Observable.throw(error.json().error || 'Server error'));
  }

news.ts:

tagesnews:Array<TagesnewsList>;

this.newsauth.gettagesnews().subscribe((dailynews)=>{
    this.tagesnews=dailynews;
  });

html:

<div *ngFor="let daily of tagesnews">
       <span style="font-size: 13px">{{daily.text}}</span> </button>
    </div>

i got response with some like this:

sample text

2

5 Answers 5

21

You just need to bind html:

<div *ngFor="let daily of tagesnews">
   <span style="font-size: 13px" [innerHTML]="daily.text"></span>
</div>
Sign up to request clarification or add additional context in comments.

4 Comments

Is it safe [innerHTML] really.
Yes, it is. Angular does send the innerHTML data through a sanitization process angular.io/guide/security#sanitization-and-security-contexts
nice and cleanest answer.
but that will render the HTML tags also , may be it served her purpose but question titile was how to remove html tags.
3

All of the [innerHTML] answers have the content of the HTML actually rendered... so images are loaded, etc. well if you don't want that, and just the text... you can use a conversion method like this:

stripHtml(html: string) {
    var div = document.createElement("DIV");

    div.innerHTML = html;

    let cleanText = div.innerText;

    div = null; // prevent mem leaks

    return cleanText;
}

Comments

2

Use replace method with regx to remove the unwanted patterns from the content.

content.replace(/<[^>]*>/g, '\n')

1 Comment

This will fail if there is a close angle bracket inside an attribute or a comment.
1

There are two ways to handle it.

1) use the innerHTML tag in your html file like this =

<div>
      <pre [innerHTML]="description"></pre>
</div>

here, description is field which hold the data with html tags(<div>hi<br></br></div>)

2) Second way is that convert your html tags data in plain string then bind with respective form control.

like :- formData['description'] = formData['description'].replace(/<[^>]*>/g, '');

Comments

0

Incase you are working with an array and the array contains html tags the easiest way to do it is as below. This might help someone who is having the text with html tags rendered from an array

<div *ngFor="let daily of tagesnews; let i = index">
    <span [innerHTML]="arrayName[i]"></span>
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.