【问题标题】:ng-bind-html vs bind-html-compile?ng-bind-html vs bind-html-compile?
【发布时间】:2016-10-11 21:05:54
【问题描述】:

我想知道 ng-bind-html 和 bind-html-compile 指令之间的区别。比如我给了

<p style='color:red'>test<p> 

对于 ng-bind-html,这去除了 bind-html-compile 没有的样式。我可以知道何时应该使用每个指令。谢谢。

【问题讨论】:

    标签: angularjs ng-bind-html angularjs-3rd-party


    【解决方案1】:

    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>';
    

    那么你需要前面提到的模块。

    【讨论】:

    • “如果找到其他目录”,您的意思是“其他指令”吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-27
    • 1970-01-01
    • 2015-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多