Safely render arbitrary HTML snippets by using ngsanitize and $SCE.
By default AngularJS Consider user's input HTML is danger, so if you want to display HTML tag on the page would show unsafe Error.
To remove this error and trust user's input, we can install Ngsanitize:
Install angular-sanitize
var egghead = Angular.module ("Egghead", ["Ngsanitize"]); Egghead.controller (function () { varthis; = ' <a href= ' Http://egghead.io "style=" color:red ">learn stuff!</strong> ';});
<!DOCTYPE HTML><HTML><Head> <title>Egghead.io</title> <Linkrel= "stylesheet"href= "Bower_components/bootstrap.css/css/bootstrap.css"/></Head><BodyNg-app= "Egghead"Ng-controller= "Appctrl as App"><textareaname=""ID=""cols= "+"rows= "Ten"Ng-model= "app.somehtml"></textarea><Divng-bind-html= "app.somehtml"></Div><Scriptsrc= "Bower_components/angular/angular.js"></Script><Scriptsrc= "Bower_components/angular-sanitize/angular-sanitize.js"></Script><Scriptsrc= "App.js"></Script></Body></HTML>
Then the error message is gone, but we didn ' t get the result which we want, we want "learn stuff" shown in red color:
<href= "Http://egghead.io" style= "color:red"> Learn stuff! </ Strong >
To overcome the We can use $SCE service:
var egghead = Angular.module ("Egghead", ["Ngsanitize"]); Egghead.controller (function ($SCE) { varthis; = $sce. trustashtml (' <a href= "Http://egghead.io" style= "color:red" >learn stuff!</strong> ');});
Also you can trust as JavaScript, CSS && URL:
See here:https://docs.angularjs.org/api/ng/service/$sce
[AngularJS] Html Ngsanitize, $sce