D
D
Dmitry2016-03-02 23:35:06
Angular
Dmitry, 2016-03-02 23:35:06

How to trim tags in json string?

Good evening. I output data from a .json file to the Angular application with the following code {{post.excerpt}} . The string contains html tags. How to remove them, leaving bare text. Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
rakro, 2016-03-03
@DimaLepel

create a filter that will format the string.

angular.module('app')
.filter('stripHTML',function(){
return function(str){
return str.replace(/<\/?[^>]+>/gi, '');
}
})

and in the template {{post.excerpt | stripHTML}}

S
Sergey, 2016-03-03
Protko @Fesor

I would recommend doing this not in filters (although you can, of course, there), but in the service where you get the data (or, again, use the filter there).
The best way to remove tags is not with a regular expression, as suggested by rakro , but using the DOM:

function stripTags(html)
{
    var tmp = document.createElement("DIV");
    tmp.innerHTML = html;
    
    return tmp.textContent || tmp.innerText;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question