How to render the content of a variable in the view? [duplicate]

0

I have a variable like the following in a controller of angular :

$scope.dato = "<h1>MI TITULO</h1>";

I want it to be shown in html , but rendered ... something like the following:

MY TITLE

In the view I put like this: {{dato}} , but what it shows is the value with the tag html

    
asked by Marck Vit 16.07.2016 в 00:07
source

3 answers

0

In the end I managed to solve the problem. The question has been asked in several places but in English. I gave the solution with answers to those questions and mainly with this question in the second answer.

Use ng-bind-html in the view, $ sce.trustAsHtml in the controller, I included the angular library-sanitize.js and in the module add ngSanitize.

    
answered by 18.07.2016 / 21:53
source
2

What happens is that you are passing everything that is going to render as a text string, so it ignores the semantic structure of HTML.

What you should do is, in your HTML, use that variable inside the h1 tag

Example:

HTML

<h1>{{dato}}</h1>

While in your JS:

$scope.dato = "Mi título";

Greetings!

    
answered by 16.07.2016 в 00:30
0

Marck Vit. Do not use the ng-bind-html. It is a very bad practice, in addition you expose your app for attacks to be able to inject code. It is available, but to use in very obligatory cases. A H1 tag is nonsense.

    
answered by 19.07.2016 в 06:36