gpt4 book ai didi

javascript - 带有 UI Bootstrap 指令的 ng-bind-html

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:45:29 24 4
gpt4 key购买 nike

我认为这不是一个直接的问题,但我不知道该怎么做。我正在尝试动态加载使用 UI Bootstrap 指令的内容,但是当加载内容时,UI Bootsrap 组件不起作用。更具体地说,工具提示不起作用。这是重要的代码:

<div ng-bind-html="trustSnippet(f.field.contentAfter)"></div>

JavaScript

$scope.trustSnippet = function(snippet) {
return $sce.trustAsHtml(snippet);
};

我要注入(inject)的 HTML 是:

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

有什么线索吗?

最佳答案

这是因为 ng-bind-html 不编译插入的元素,因此 UI Bootstrap 指令 - 或任何其他指令或表达式,就此而言,将不起作用。

如果您从特定位置获取 HTML,您可以简单地使用 ng-include

对于静态位置:

<div ng-include="'path/to/html'"></div>

或者,如果位置是动态的并且存储在作用域公开的变量中:$scope.path = "path/to/html";:

<div ng-include="path"></div>

否则,如果带有 Angular 表达式/指令的 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);
})
}
};
});

不要忘记添加 "ngSanitize" 作为依赖项。用法是:

<div ng-bind-html-compile="html"></div>

关于javascript - 带有 UI Bootstrap 指令的 ng-bind-html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28465352/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com