Ali
Ali

Reputation: 10463

How to output HTML in angularjs with just using filter

I'm able to unescape the string that has some markup in it using ngSanitize and in HTML I use

<span ng-bind-html="myHTML"></span>

I've created a filter

.filter('safe', function($sce) {
    return function(val) {
      return $sce.trustAsHtml(val);
  };
})

I wonder if I can do something like this instead?

<span>{{ myHTML | safe }}</span>

Right now it does not really work and I'm trying to see what I'm missing.

Upvotes: 0

Views: 89

Answers (1)

GonchuB
GonchuB

Reputation: 705

You can make your own filter for that:

var app = angular.module('yourModuleName');
app.filter('safe', function($sce) {
    return function(htmlString) {
        return $sce.trustAsHtml(htmlString);
    }
};

Your markup would be something like:

<span ng-bind-html="myHTML | safe"></span>

Upvotes: 2

Related Questions