Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to render html with AngularJS templates

This is my template:

<div class="span12">   <ng:view></ng:view> </div> 

and this is my view template:

<h1>{{stuff.title}}</h1>  {{stuff.content}} 

I am getting the content as html and I want to display that in a view, but all I am getting is raw html code. How can I render that HTML?

like image 250
user194932147 Avatar asked Apr 02 '13 01:04

user194932147


People also ask

Can I use HTML template in Angular?

An Angular HTML template renders a view, or user interface, in the browser, just like regular HTML, but with a lot more functionality. When you generate an Angular application with the Angular CLI, the app. component. html file is the default template containing placeholder HTML.

Which HTML tag is used to define the templates in AngularJS?

A static template is defined by using script tag. It must has an id attribute with a unique value and a type attribute with value text/ng-template .

What is AngularJS template URL?

templateUrl can also be a function which returns the URL of an HTML template to be loaded and used for the directive. AngularJS will call the templateUrl function with two parameters: the element that the directive was called on, and an attr object associated with that element.


2 Answers

Use-

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

You need to tell angular to not escape it.

like image 171
Jason Avatar answered Sep 28 '22 09:09

Jason


To do this, I use a custom filter.

In my app:

myApp.filter('rawHtml', ['$sce', function($sce){   return function(val) {     return $sce.trustAsHtml(val);   }; }]); 

Then, in the view:

<h1>{{ stuff.title}}</h1>  <div ng-bind-html="stuff.content | rawHtml"></div> 
like image 33
Daniel Avatar answered Sep 28 '22 10:09

Daniel