【问题标题】:ng-bind-html on a string containing angular directiveng-bind-html 在包含 angular 指令的字符串上
【发布时间】:2014-04-03 11:37:27
【问题描述】:

我正在使用 Angular ng-bind-html 向当前元素添加控件。当我这样做时,会添加控件,但缺少字符串中指定的任何角度指令。例如

原始字符串'<a ng-click="hello()" style="color:#8FA2AF">Hello</a>'; 结果控制<a style="color:#8FA2AF">Hello</a>

谁能帮忙?

html

<span >
      <p ng-bind-html="sample_text"> 
</span>

控制器

mymodule.controller('MyController',function MyController($scope) {
$scope.sample_text='<a ng-click="hello()" style="color:#8FA2AF">Hello</a>';
}

【问题讨论】:

    标签: html angularjs angularjs-directive


    【解决方案1】:

    你应该像这样使用 $sce.trustAsHtml()...

    $scope.sample_text= $sce.trustAsHtml('`enter code here`<a ng-click="hello()" style="color:#8FA2AF">Hello</a>');
    

    但是这样你不能绑定范围变量(比如你的hello()函数)到你的html,所以最好的方法是写一个指令......

    PLUNKER 正在为 $sce 和指令示例工作...

    【讨论】:

      猜你喜欢
      • 2013-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多