角度消毒的目的是什么? [英] What is the purpose of angular-sanitize ?

查看:104
本文介绍了角度消毒的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚查看了角度和开放层指令的示例 HERE 并遇到以下示例:

I was just going through the examples for a angular and openlayers directive HERE and came across the following example:

<!DOCTYPE html>
<html ng-app="demoapp">
  <head>
    <script src="../bower_components/openlayers3/build/ol.js"></script>
    <script src="../bower_components/angular/angular.min.js"></script>
    <script src="../bower_components/angular-sanitize/angular-sanitize.min.js"></script>
    <script src="../dist/angular-openlayers-directive.js"></script>
    <link rel="stylesheet" href="../bower_components/openlayers3/build/ol.css" />
    <script>
        var app = angular.module('demoapp', ['openlayers-directive']);
        app.controller('DemoController', [ '$scope', function($scope) {
            angular.extend($scope, {
                center: {
                    lat: 0,
                    lon: 0,
                    autodiscover: true
                }
            });
       }]);
    </script>
  </head>
  <body ng-controller="DemoController">
    <openlayers ol-center="center"  height="400px"></openlayers>
    <h1>Center autodiscover example</h1>
    <form>
        Latitude : <input type="number" step="any" ng-model="center.lat" />
        Longitude : <input type="number" step="any" ng-model="center.lon" />
        Zoom : <input type="number" step="any" ng-model="center.zoom" />
        <button ng-click="center.autodiscover=true">Discover position</button>
    </form>
  </body>
</html>

该示例可视为实例 HERE

The example can be seen as a live example HERE.

我的问题是关于正在加载的文件,我不太明白为什么加载下面的脚本:

My question is about the files being loaded, I don't quite understand why the below script being loaded:

<script src="../bower_components/angular-sanitize/angular-sanitize.min.js"></script>

上述脚本的目的是什么?

What is the purpose of the above script ?

: :我发现这个模块的git repo和文档的角度为 HERE 。 ,但我仍然不明白这个脚本的目的,文档甚至没有一个例子。

: : i found out the git repo and the docs here for this module in angular HERE. , but i still don't understand the purpose of this script , the documentation does't even have a single example.

我已经在jQuery中编写了相当多的代码,那么有人可以用jQuery术语来解释这个吗?

I have coded in jQuery a fair bit , so can somebody explain this in jQuery terms ?

推荐答案

如果包含 angular-sanitize 脚本,则会对输入进行清理通过将HTML解析为标记。然后将所有安全令牌(来自白名单)序列化为正确转义的html字符串。这意味着没有不安全的输入可以使它进入返回的字符串。

If you include the angular-sanitize script, inputs are sanitized by parsing the HTML into tokens. All safe tokens (from a whitelist) are then serialized back to properly escaped html string. This means that no unsafe input can make it into the returned string.

我在下面列出了一个小例子,受到了这个博客文章。如果您使用 var app = angular.module(app,[ngSanitize])运行此脚本; 正确呈现html链接。但是,如果您将此语句注释掉并取消注释 var app = angular.module(app,[]); 将引发以下错误消息:错误:[$ sce:unsafe]试图在安全的上下文中使用不安全的值。

I have included a small example below inspired by this blog post. If you run this script with var app = angular.module("app", ["ngSanitize"]); the html links are rendered correctly. However, if you comment this statement out and uncomment var app = angular.module("app", []); the following error message is raised: Error: [$sce:unsafe] Attempting to use an unsafe value in a safe context.

<!DOCTYPE html>
<html>

<head>
  <link rel="icon" type="image/x-icon" href="favicon.ico">

  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.1/angular.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.1/angular-sanitize.min.js"></script>
  <!-- BEGIN disable refresh -->
  <script type="text/javascript">
    //Including ngSanitize ensures html links get properly sanitized
    var app = angular.module("app", ["ngSanitize"]);
    //If you use this code instead no html links get displayed 
    //var app = angular.module("app", []);

    app.controller("mainController", function($scope) {
      var main = this;

      main.links = [
        "<a href='http://google.com'>Google</a>",
        "<a href='http://odetocode.com'>OdeToCode</a>",
        "<a href='http://twitter.com'>Twitter</a>"
      ];
    });
  </script>

</head>

<body ng-app="app">
  <section ng-controller="mainController as main">
    <nav>
      <ul>
        <li ng-repeat="link in main.links" ng-bind-html="link">
        </li>
      </ul>
    </nav>
  </section>
</body>

</html>

这篇关于角度消毒的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆