[AngularJS] Html Ngsanitize, $sce

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.