angularjs - 哪个表达式不会通过 $sce.trustAsHtml 清理?

标签 angularjs ng-bind-html

假设我有 -

$scope.trustAsHtml = $sce.trustAsHtml; 

<p ng-bind-html="trustAsHtml(expression)"></p>

trustAsHtml 可以检查什么,使其表达式 不会显示为信任 HTML?

请给我一些例子。

最佳答案

严格上下文转义(SCE)是一种模式,在这种模式下,AngularJS 需要在某些上下文中进行绑定(bind),以生成一个被标记为可安全用于该上下文的值。这种上下文的一个例子是通过 ng-bind-html 绑定(bind)由用户控制的任意 html。我们将这些上下文称为特权上下文或 SCE 上下文。

Note : If you use normal html using directly bind in html using ng-bind-html. if you have some special characters found in the html means i am suggested use $sce.trustAsHtml() and bind it

例如

应该是:

<div ng-bind-html="trustedHtml"></div>

在 Controller 中加上:

$scope.html = '<span onmouseover="this.textContent=&quot;Explicitly trusted HTML bypasses sanitization.&quot;">Hover over this text.</span>';
$scope.trustedHtml = $sce.trustAsHtml($scope.html);

关于angularjs - 哪个表达式不会通过 $sce.trustAsHtml 清理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30775142/

相关文章:

javascript - AngularJs 发送 OPTION 而不是 POST 请求

php - 列出 3 个时 ANGULARJS 结果消失

javascript - 如何通过 JavaScript 函数创建 Angular JS Material 元素?

AngularJS : The correct way of binding to a service properties

javascript - 如何在 ng-bind-html 中呈现 Angular 变量的内容

javascript - Angular ng-bind-html ng-bind 更多 Angular 代码?

angularjs - 复制并替换 Electron 版中的数据库文件

javascript - ng-click 不适用于 ng-bind-html

javascript - ng-bind-html 不适用于脚本

javascript - 无法在 AngularJS ng-views 中加载 Instagram 配置文件