1

In jQuery, if you have a string with HTML in it you can process the HTML in it and replace special characters like '&' with '&'

var myString = 'Jack & Jill';
var filtered = $('</div>').html(myString).text(); 
console.log(filtered); // outputs 'Jack & Jill'

I am writing a filter which would process HTML, but I'm depending on jQuery here, is there an AngularJS way to do it?

Here is my Filter code:

myApp.filter('filterHtmlChars', function() {
    return function(html) {
        return $('<div/>').html(html).text(); // how could I use AngularJS here?
    };
});

PS: I already know about ng-bind-html, what I'm trying to do is filter.

1
  • jqLite would do the trick if your main goal is to avoid loading jQuery: plnkr.co/edit/ce7g2p64JO4CahakupYt?p=preview However, I think you might be asking for a way that doesn't involve using a stand-in element altogether? Commented May 19, 2014 at 20:38

1 Answer 1

6

Use built-in jqLite instead:

.filter('filterHtmlChars', function(){
   return function(html) {
       var filtered = angular.element('<div>').html(html).text(); 
       return filtered;
   }
});

Then, in your view, {{ foo | filterHtmlChars}} outputs "Jack & Jill where $scope.foo = 'Jack &amp; Jill'

Plunker Demo

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.