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 will show unsafe error.

To remove this error and trust user's input, we can install ngSanitize:

bower install angular-sanitize
var egghead = angular.module("egghead", ["ngSanitize"]);

egghead.controller("AppCtrl", function () {
var app = this; app.someHtml = '<a href="http://egghead.io" style="color:red">Learn stuff!</strong>';
});
<!DOCTYPE html>
<html>
<head>
<title>Egghead.io</title>
<link rel="stylesheet" href="bower_components/bootstrap.css/css/bootstrap.css"/>
</head>
<body ng-app="egghead" ng-controller="AppCtrl as app">
<textarea name="" id="" cols="30" rows="10" ng-model="app.someHtml"></textarea>
<div ng-bind-html="app.someHtml"></div>
<script src="bower_components/angular/angular.js"></script>
<script src="bower_components/angular-sanitize/angular-sanitize.js"></script>
<script src="app.js"></script>
</body>
</html>

Then the error message has gone, but we didn't get the result which we want, we want "Learn stuff" shown  in red color:

<a href="http://egghead.io" style="color:red">Learn stuff!</strong>

To overcome this, we can use $sce service:

var egghead = angular.module("egghead", ["ngSanitize"]);

egghead.controller("AppCtrl", function ($sce) {
var app = this; app.someHtml = $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的更多相关文章

  1. 5、AngularJS 直接绑定显示html ($sce、$sanitize服务)

    1.直接使用$sce服务(angularjs中:$sce.trustAsHtml($scope.snippet).html:ng-bind-html="snippet") 以下代码 ...

  2. 《AngularJS学习整理》系列分享专栏

    <AngularJS学习整理>系列分享专栏   <AngularJS学习整理>已整理成PDF文档,点击可直接下载至本地查阅https://www.webfalse.com/re ...

  3. ngBind ngBindTemplate ngBindHtml

    ng-bind: 只能绑定一个变量 在AngularJS中显示模型中的数据有两种方式: 一种是使用花括号插值的方式: <p>{{titleStr}}</p> 另一种是使用基于属 ...

  4. 前端安全(XSS、CSRF防御)

    一.网络安全          OWASP:开放式Web应用程序安全项目(OWASP,Open Web Application Security Project)        OWASP是一个开源的 ...

  5. AngularJs $sce 和 $sceDelegate 上下文转义

    $sce $sce 服务是AngularJs提供的一种严格上下文转义服务. 严格的上下文转义服务 严格的上下文转义(SCE)是一种需要在一定的语境中导致AngularJS绑定值被标记为安全使用语境的模 ...

  6. AngularJS 使用$sce控制代码安全检查

    由于浏览器都有同源加载策略,不能加载不同域下的文件.也不能使用不合要求的协议比如file进行访问. 在angularJs中为了避免安全漏洞,一些ng-src或者ng-include都会进行安全校验,因 ...

  7. 【AngularJs】---$sce 输出Html

    [问题描述] angular js的强大之处之一就是他的数据双向绑定功能----->ng-bind和针对form的ng-model 但在我们的项目当中会遇到这样的情况,后台返回的数据中带有各种各 ...

  8. AngularJS通过$sce输出html的方法

    不知道大家有没有发现在用AngularJS作为前端搭建个人博客的时候,发现用AngularJs输出html的时候,浏览器并不解析这些html标签,这里我们需要其显示angular输出的html能被浏览 ...

  9. angularjs通过ng-bind-html指令和$sce服务绑定html

    代码: <!doctype html> <html lang="en"> <head> <meta charset="UTF-8 ...

随机推荐

  1. C# Adding Hyperlink to Windows Form z

    When creating a Windows form in C#, we would like to create a hyperlink so that when the user click ...

  2. Linux基本命令(4)有关关机和查看系统信息的命令

    有关关机和查看系统信息的命令 命令 说明 shutdown 正常关机 reboot 重启计算机 ps 查看目前程序执行的情况 top 查看目前程序执行的情景和内存使用的情况 kill 终止一个进程 d ...

  3. linux的文件属性介绍、目录及路径表示方法

    一.认识linux文件 认识linux下的文件需要先学习命令:ls. 该命令用于显示指定目录下的内容,其中最常用的参数有: -l显示目录和文件的完整属性信息 -a显示所有文件和目录,包含隐藏文件和目录 ...

  4. DataGird导出EXCEL的几个方法

    DataGird导出EXCEL的几个方法(WebControl) using System;using System.Data;using System.Text;using System.Web;u ...

  5. Java内部类this$0字段产生的一个bug

    首先查看下面一段代码,我指出了问题代码的所在,读者先自己思考一下这段代码会有什么问题. 这是用clone方法完整拷贝一个二项堆(BinomialHeap)结构的代码.二项堆中包含一个内部类Binomi ...

  6. Bias/variance tradeoff

    线性回归中有欠拟合与过拟合,例如下图: 则会形成欠拟合, 则会形成过拟合. 尽管五次多项式会精确的预测训练集中的样本点,但在预测训练集中没有的数据,则不能很好的预测,也就是说有较大的泛化误差,上面的右 ...

  7. Cloudera CDH5 部署实战指南(离线安装)

    配置软件源服务器 1.安装createreporpm -ivh deltarpm-3.5-0.5.20090913git.el6.x86_64.rpm rpm -ivh python-deltarpm ...

  8. 【hadoop代码笔记】Hadoop作业提交中EagerTaskInitializationListener的作用

    在整理FairScheduler实现的task调度逻辑时,注意到EagerTaskInitializationListener类.差不多应该是job提交相关的逻辑代码中最简单清楚的一个了. todo: ...

  9. JAVA 基础 重新开始

    之前做android开发,因为JAVA基础不牢固的原因,自己对写代码很不自信,很多时候要去找源码或者在相近的代码上修修改改以得到想要的结果,从某种意义上来说这根本算不上真正意义上的程序员.后来看到某位 ...

  10. Gym 100507D Zhenya moves from the dormitory (模拟)

    Zhenya moves from the dormitory 题目链接: http://acm.hust.edu.cn/vjudge/contest/126546#problem/D Descrip ...