【发布时间】:2024-01-22 18:19:01
【问题描述】:
我的作用域中有一个字符串,但我并不总是知道是否要转义 HTML。本质上,我有一个布尔值,它会说明是否应该转义 HTML。
代码
这是我的一些示例代码:
$scope.result = "<b>foo</b>bar";
$scope.html = false; // In this case the HTML *would* be escaped
这是一个插入 HTML 的情况,如 innerHTML:
$scope.result = "<strike>foo</strike>bar";
$scope.html = true; // The HTML would be escaped
我尝试过的其他解决方案
我不确定 “Angular 方式” 会是什么,虽然我曾想过使用 .removeAttribute 进行黑客攻击,但我觉得这非常 hacky,必须有更好的方法。
【问题讨论】:
-
为什么不使用一个指令来传递一个指示 HTML 是否被转义的属性?像这样
标签: javascript html angularjs ng-bind-html ng-bind