如何让 ng-bind-html 编译 angularjs 代码

IT技术 javascript html angularjs web
2021-01-16 02:30:28

我正在使用 angularjs 1.2.0-rc.3。我想动态地将 html 代码包含到模板中。为此,我在控制器中使用:

html = "<div>hello</div>";
$scope.unicTabContent = $sce.trustAsHtml(html);

在我得到的模板中:

<div id="unicTab" ng-bind-html="unicTabContent"></div>

它适用于常规 html 代码。但是当我尝试放置 angular 模板时,它不会被解释,它只是包含在页面中。例如,我想包括:

<div ng-controller="formCtrl">
    <div ng-repeat="item in content" ng-init="init()">
    </div>
</div>

非常感谢

6个回答

此解决方案不使用硬编码模板,您可以编译嵌入在 API 响应中的 Angular 表达式。


步骤 1. 安装此指令:https : //github.com/incuna/angular-bind-html-compile

步骤 2.在module中包含指令。

angular.module("app", ["angular-bind-html-compile"])

步骤 3.在模板中使用指令:

<div bind-html-compile="letterTemplate.content"></div>

结果:

控制器对象

 $scope.letter = { user: { name: "John"}}

JSON 响应

{ "letterTemplate":[
    { content: "<span>Dear {{letter.user.name}},</span>" }
]}

HTML 输出 =

<div bind-html-compile="letterTemplate.content"> 
   <span>Dear John,</span>
</div>

作为参考,这里是相关指令:

(function () {
    'use strict';

    var module = angular.module('angular-bind-html-compile', []);

    module.directive('bindHtmlCompile', ['$compile', function ($compile) {
        return {
            restrict: 'A',
            link: function (scope, element, attrs) {
                scope.$watch(function () {
                    return scope.$eval(attrs.bindHtmlCompile);
                }, function (value) {
                    element.html(value);
                    $compile(element.contents())(scope);
                });
            }
        };
    }]);
}());
这个 jsfiddle 有助于说明ng-bind-html之间的编译差异bind-html-compilejsfiddle.net/HB7LU/14557
2021-03-17 02:30:28
我想在我意识到这将如何简单地解决我的问题之前,我问了三个不同的问题。如果可以的话,我会投票两次。
2021-04-05 02:30:28

这就是我所做的,不知道它是否是角度方式TM,但它有效并且非常简单;

.directive('dynamic', function($compile) {
    return {
        restrict: 'A',
        replace: true,
        link: function (scope, element, attrs) {
            scope.$watch(attrs.dynamic, function(html) {
                $compile(element.html(html).contents())(scope);
            });
        }
    };
});

所以;

<div id="unicTab" dynamic="unicTabContent"></div>

编辑:我找到了 angular way,它非常简单。

$templateCache.put('unicTabContent', $sce.trustAsHtml(html));
<div id="unicTab" ng-include="'unicTabContent'"></div>

不需要制定自己的指令或任何东西。但这是一种绑定一次的交易,它不会自定义指令那样看到对 html 所做的更改

我发现这个答案最容易实现。
2021-03-18 02:30:28
嘿,为什么在这里使用 $watch?为什么不直接用编译的内容替换元素?谢谢顺便说一句。
2021-04-02 02:30:28
这样,如果变量发生更改,该更改将反映在下一个 DOM 中digest如果你的变量永远不会改变,你可以简化这个,当然,但每当我提出答案时,我都会尽量保持它的通用性和普遍适用性。对于我的特定情况,我认为当指令被评估时我的变量是空的,所以我需要watchfor 当变量最终被填充时。
2021-04-02 02:30:28
这对我来说效果更好!尤其是如果您还使用带有元素的过滤器,那么这绝对是您要走的路。如果使用 angular-bind -html-compile 东西,我会达到摘要限制
2021-04-07 02:30:28
有什么办法可以使 ng-html-compile 有条件?我的意思是仅在特定条件下附加/分配指令,例如 - 网格行单击并正常运行?
2021-04-09 02:30:28

正如 Vinod Louis 在他的评论中所说,最好的方法是使用模板。我必须在常规代码之外定义一个模板,例如我在 index.html 中添加了该代码:

<script type="text/ng-template" id="unic_tab_template.html">
    <div ng-switch on="page">
        <div ng-switch-when="home"><p>{{home}}</p></div>
        <div ng-switch-when="form">
            <div ng-controller="formCtrl">
                <div ng-repeat="item in content">{{item.name}}:{{item.value}}</div>
            </div>
        </div>
        <div ng-switch-default>an error accured</div>
    </div>
</script>

此模板是有条件的,因此根据 $scope.page 的值,它会在 3 个模板(第三个是错误处理程序)之间切换。要使用它,我有:

<div id="unicTab" ng-controller="unicTabCtrl">
    <div ng-include="'unic_tab_template.html'"></div>
</div>

这样我的页面会根据 unicTabCtrl 控制器中的 $scope 改变。

总结插入 angularsjs 模板接缝的想法难以实现($compile 接缝是解决方案,但我无法使其工作)。但是,您可以使用条件模板。

我喜欢这个解决方案,但如果我们能看到一个示例,其中 HTML 来自其他地方(例如远程 JSON,因此没有在项目中进行硬编码),那就更好了。
2021-04-02 02:30:28

我试图做同样的事情并遇到了这个module。

http://ngmodules.org/modules/ng-html-compile

我只是包含它,然后我就可以使用“ng-html-compile”而不是“ng-bind-html”

一种方法是使用指令来插入包含角度表达式的自定义模板

<div id="unicTab" unic-tab-content></div>
app.directive("unicTabContent",function(){
   return {
      restrict:"A",
      template:'{{unicTabContent}}'
   }
})
我喜欢这个想法,它确实包含代码,但仍未编译。
2021-03-26 02:30:28
但是我希望它编译您称为 test 的变量中的代码。例如,plunker显示 <div> 和 </div>,这不是我想要的。
2021-03-27 02:30:28
OK..可以用link指令Plunker的回调 来做
2021-03-30 02:30:28
在这里工作正常,因为 plnkr.co/edit/sUOxHBUYIdQlAiM3Dy0V?p=preview 也许您有一些嵌套指令或其他原因导致它无法工作
2021-04-09 02:30:28
实际上它编译 html,但不是 angular 语句。Plunker 中,当直接注入测试变量时,ng-click 方法是连接的,但是当使用指令注入时,它不是。
2021-04-10 02:30:28