我想知道ng-bind-html和bind-html-compile指令之间的区别。例如我给了
<p style='color:red'>test<p>
到ng-bind-html,这剥离了bind-html-compile没有的样式。我可以知道何时应该使用每个指令。感谢。
答案 0 :(得分:5)
bind-html-compile 不是标准的Angular指令,它带有模块https://github.com/incuna/angular-bind-html-compile,它用于编译绑定数据。为了简单起见,它相当于在源代码中编写html:它将被重新评估,如果找到其他指令,它们将按预期工作。
ng-bind-html 是一个标准指令(与Angular本身捆绑在一起),只输出html字符串而不编译。
例如,如果你的控制器有一个带有普通html的变量,比如:
$scope.dataToDisplay = '<h1><strong>Title</strong></h1>';
然后你可以使用ng-bind-html
。
如果需要将包含html的变量与其他指令一起注入,例如:
$scope.dataToDisplay = '<h1 ng-show="showIfOtherVariable"><strong>Title</strong></h1>';
然后你需要上述模块。