Showing posts with label html sanitize. Show all posts
Showing posts with label html sanitize. Show all posts

Saturday, December 17, 2016

Rendering text to html with Angular JS

Download example code from here


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Rendering text to html with Angular JS</title>
    <script src="angular.js"></script>
    <script src="angular-sanitize.js"></script>
</head>
<body>

<div data-ng-app="myApp" data-ng-controller="MyController">
    <div ng-bind-html="myHTML()"></div>
</div>

<script type="text/javascript">
    var app = angular.module("myApp", ["ngSanitize"]);

    app.controller("MyController", function ($scope, $sce) {
        $scope.snippet = "<div>This is a DIV element; alert('SURE???')</div>";
        $scope.myHTML = function () {
            return $sce.trustAsHtml($scope.snippet);
        }
    });
</script>

</body>
</html>