Mayukh Raaj
Mayukh Raaj

Reputation: 403

How to render special characters like ™ in HTML with Angular JS

I have some special characters to show in various screens of my application so i wanted to have some way where i can handle special characters like "special char - æ ™ &amp" in controller/service instead of HTML.

I know i can use

ng-bind-html

to show special characters for the example string above. However i need to show same string in multiple screens so it would make more sense for me to do it in JS. Any alternative or equivalent JS side snippet for ng-bind-html?

Note: If you have come across these kind of strings you might be knowing that they can be rendered directly with HTML but if you are using Angular JS with

{{some scope value}}

then it doesn't format special characters on it's own.

Upvotes: 1

Views: 4251

Answers (1)

bamtheboozle
bamtheboozle

Reputation: 6436

You can use $sce like so:

function myCtrl($scope,$sce){
    $scope.html = $sce.trustAsHtml('HTML_CODE;');
}

And then in your HTML you use ng-bind-html to bind the content to an element.

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

Upvotes: 2

Related Questions