I am creating an Angular JS app and i get a text from the server that i need to display in my site. The text i got from server :
"My name is <b> John </b>" with b tags.
Unfortunately, when I display it every one notify the strong text tags.
{{text}}
I would like that users will see only "John" in bold. Any ideas?
That's normal behavior, it's happening because of Angular's sanitize. You can't just print out html tags. You need to use ng-bind-html.
You can use ng-bind-html
as @CAT suggested, but if you don't want to go as much as use $sce.trushAsHtml()
explicitly you can use this directive:
.directive('htmlSafe', function($parse, $sce) {
return {
link: function(scope, elem, attr) {
var html = attr.ngBindHtml;
if(angular.isDefined(html)) {
var getter = $parse(html);
var setter = getter.assign;
setter(scope, $sce.trustAsHtml(getter(scope)));
}
}
};
});
and then you can use it like this in your html:
<div ng-bind-html="html" html-safe></div>