javascript tutorial - [Solved-5 Solutions] Insert HTML into view - javascript - java script - javascript array



Problem:

Is it possible to create an HTML fragment in an AngularJS controller and have this HTML shown in the view ?

This comes from a requirement to turn an inconsistent JSON blob into a nested list of id : valuepairs. Therefore the HTML is created in the controller and now we looking to display it. We have created a model property, but cannot render this in the view without it just printing the HTML.

Update

It appears that the problem arises from angular rendering the created HTML as a string within quotes. Will attempt to find a way around this.

Example controller :

var SomeController = function () {

    this.customHtml = '<ul><li>render me please</li></ul>';
}
click below button to copy the code. By JavaScript tutorial team

Example view :

<div ng:bind="customHtml"></div>
click below button to copy the code. By JavaScript tutorial team

Gives :

<div>
    "<ul><li>render me please</li></ul>"
</div>

click below button to copy the code. By JavaScript tutorial team

Solution 1:

For Angular 1.x, use ng-bind-html in the HTML:

<div ng-bind-html="thisCanBeusedInsideNgBindHtml"></div>
click below button to copy the code. By JavaScript tutorial team

At this point we would get a attempting to use an unsafe value in a safe context error so we need to either use ngSanitize or $sce to resolve that. $sce Use $sce.trustAsHtml() in the controller to convert the html string.

 $scope.thisCanBeusedInsideNgBindHtml = $sce.trustAsHtml(someHtmlVar);
click below button to copy the code. By JavaScript tutorial team

ngSanitize

There are 2 steps:

  • Include the angular-sanitize.min.js resource, i.e.:
  • <script> src="lib/angular/angular-sanitize.min.js"></script>
  • In a js file (controller or usually app.js), include ngSanitize, i.e.:
  • angular.module('myApp', ['myApp.filters', 'myApp.services', 'myApp.directives', 'ngSanitize'])

Solution 2:

We can also create a filter like so:

var app = angular.module("demoApp", ['ngResource']);

app.filter("trust", ['$sce', function($sce) {
  return function(htmlCode){
    return $sce.trustAsHtml(htmlCode);
  }
}]);
click below button to copy the code. By JavaScript tutorial team

Then in the view

<div ng-bind-html="trusted_html_variable | trust"></div>
click below button to copy the code. By JavaScript tutorial team

Solution 3:

The solution provided in the above link worked for me, none of the options on this thread did. For anyone looking for the same thing with AngularJS version 1.2.9 Here's a copy: Ok we found solution for this: JS:

$scope.renderHtml = function(html_code)
{
    return $sce.trustAsHtml(html_code);
};
click below button to copy the code. By JavaScript tutorial team

HTML:

<p ng-bind-html="renderHtml(value.button)"></p>
click below button to copy the code. By JavaScript tutorial team

EDIT: Here's the set up: JS file:

angular.module('MyModule').controller('MyController', ['$scope', '$http', '$sce',
    function ($scope, $http, $sce) {
        $scope.renderHtml = function (htmlCode) {
            return $sce.trustAsHtml(htmlCode);
        };

        $scope.body = '<div style="width:200px; height:200px; border:1px solid blue;"></div>'; 

    }]);
click below button to copy the code. By JavaScript tutorial team

HTML file:

<div ng-controller="MyController">
    <div ng-bind-html="renderHtml(body)"></div>
</div>
click below button to copy the code. By JavaScript tutorial team

Solution 4:

Fortunately, we don't need any fancy filters or unsafe methods to avoid that error message. This is the complete implementation to properly output HTML markup in a view in the intended and safe way. The sanitize module must be included after Angular:

<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular-sanitize.js"></script>
click below button to copy the code. By JavaScript tutorial team

Then, the module must be loaded:

angular.module('app', [
  'ngSanitize'
]);
click below button to copy the code. By JavaScript tutorial team

This will allow we to include markup in a string from a controller, directive, etc:

scope.message = "<strong>42</strong> is the <em>answer</em>.";
click below button to copy the code. By JavaScript tutorial team

Finally, in a template, it must be output like so:

<p ng-bind-html="message"></p>
click below button to copy the code. By JavaScript tutorial team

Solution 5:

We have tried today, the only way we found was this

<div ng-bind-html-unsafe="expression"></div>
click below button to copy the code. By JavaScript tutorial team

Related Searches to javascript tutorial - Insert HTML into view