允许 HTML 的角度树视图中的自定义标签

Custom Label in angular-ivh-treeview that allows for HTML

本文关键字:自定义 视图 标签 HTML 允许      更新时间:2023-09-26

我正在使用 https://github.com/ivantage/angular-ivh-treeview/blob/master/docs/templates-and-skins.md#tree-layouts标签内的 HTML 或使用自定义节点字段绑定到 ng-bind-html 时遇到问题。 所以我尝试制作一个自定义指令来替换标签字段,但我似乎无法让它工作。

有没有办法使用 ng-sanitize 让标签绑定为 html,以便我可以将 html 标记放在标签中?

如果您的标签已经嵌入了 HTML,最简单的方法是使用 ivhTreeviewBfs 服务遍历您的树,并明确信任每个节点的标签$sce.trustAsHtml。然后,您可以根据需要在模板中使用ng-bind-html="trvw.lable(node)"

下面是一个演示来说明: http://jsbin.com/bogoyu/2/edit?js,output

请注意,如果您不想将 html 放在标签中,也可以根据节点属性(即 ng-class="{fancy: node.foo = '...'}" 等)向模板添加条件。

除了 Justin 的精彩回答之外,您还可以使用 angular 的 ngSanitize 模块来"清理"html。

只需包含角度清理,并ngSanitize作为应用程序模块的依赖项。前任:

var app = angular.module("myApp", ['ngSanitize']);

使用 ng-bind-html 绑定的作用域变量会自动清理。无需遍历树或使用$sce服务