Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用UI引导指令绑定html_Javascript_Angularjs_Angularjs Directive_Angular Ui Bootstrap - Fatal编程技术网

Javascript 使用UI引导指令绑定html

Javascript 使用UI引导指令绑定html,javascript,angularjs,angularjs-directive,angular-ui-bootstrap,Javascript,Angularjs,Angularjs Directive,Angular Ui Bootstrap,我不认为这是一个直接的问题,但我不知道如何做到这一点。我试图动态加载使用UI引导指令的内容,但加载内容时,UI引导RAP组件无法工作。更具体地说,工具提示不起作用。以下是重要的代码: <div ng-bind-html="trustSnippet(f.field.contentAfter)"></div> 我尝试注入的HTML是: <i class="fa fa-exclamation-circle" tooltip-placement="right" toolt

我不认为这是一个直接的问题,但我不知道如何做到这一点。我试图动态加载使用UI引导指令的内容,但加载内容时,UI引导RAP组件无法工作。更具体地说,工具提示不起作用。以下是重要的代码:

<div ng-bind-html="trustSnippet(f.field.contentAfter)"></div>
我尝试注入的HTML是:

<i class="fa fa-exclamation-circle" tooltip-placement="right" tooltip="On the Right!"></i>

有什么线索吗


TY

这是因为
ng bind html
不编译插入的元素,因此UI引导指令或任何其他指令或表达式都无法工作

如果您从特定位置获取HTML,只需使用
nginclude

对于静态位置:

<div ng-include="'path/to/html'"></div>
否则,如果动态生成或导入带有角度表达式/指令的HTML本身(这是一种罕见的情况,您应该重新检查您的设计以确保没有违反任何最佳实践),则需要使用
$compile
服务编译它,最好使用指令:

app.directive("ngBindHtmlCompile", function($compile, $sce){
  return {
    restrict: "A",
    link: function(scope, element, attrs){
      scope.$watch($sce.parseAsHtml(attrs.ngBindHtmlCompile), function(html){
        var el = angular.element("<div>").html(html);
        element.empty();
        element.append(el.children());
        $compile(element.contents())(scope);
      })
    }
  };
});

我也面临同样的问题。以下方法对我有效

在HTML中

<div ng-bind-html="f.field.contentAfter | unsafe"></div>

提供最小的、可运行的示例。就目前而言,这是无法回答的。是的,谢谢你的回答。我必须每周解决一次我的问题,并得到与您提供的相同的答案。我每次都要编译html。无论如何,谢谢你的解释,现在我有了更深的理解。@dsaket的答案应该是公认的答案,因为这更干净。即使不添加“val”,返回仍然有效。app.filter('safe',函数($sce){return$sce.trustAsHtml;});
app.directive("ngBindHtmlCompile", function($compile, $sce){
  return {
    restrict: "A",
    link: function(scope, element, attrs){
      scope.$watch($sce.parseAsHtml(attrs.ngBindHtmlCompile), function(html){
        var el = angular.element("<div>").html(html);
        element.empty();
        element.append(el.children());
        $compile(element.contents())(scope);
      })
    }
  };
});
<div ng-bind-html-compile="html"></div>
<div ng-bind-html="f.field.contentAfter | unsafe"></div>
app.filter('unsafe', function($sce) {
  return function(val) {
      return $sce.trustAsHtml(val);
  }; 
});