什么时候使用 ngsanitize?

When to use ngsanitize?

我需要保护我的数据免受 xss 漏洞的影响。

是否只有在同时使用 ng-bind-html 或其他情况下才需要使用 ngsanitize

您不必清理您知道是安全的数据。要明确信任一个值,您可以使用 $sce.trustAsHtml(value)

If a $sanitize service is unavailable and the bound value isn't explicitly trusted, you will have an exception (instead of an exploit.)

此处来源:https://docs.angularjs.org/api/ng/directive/ngBindHtml

编辑:

NgSanitize 转义您的数据,以便您可以安全地在 html 页面上呈现它。它可以防止任何恶意注入的代码在您的页面上执行。这涉及动态数据和用户输入(表单、url 参数、...)。静态内容不需要清理。您可以在此处找到有用用法的示例:

没有 ng-bind-html 你不需要 ng-sanitize。只要您的页面上没有呈现任何其他动态数据或用户输入。