【问题标题】:Compiling HTML in Angular JS Directive在 Angular JS 指令中编译 HTML
【发布时间】:2017-05-28 04:03:58
【问题描述】:

我正在尝试为一个简单的模式窗口构建以下指令,其用法如下:

<modal content="<div class='error-text'>this is the content</div>"></modal>

应该生成以下标记:

<div class="modal-wrap fade-hide fade-show" ng-show="showModal" ng-click="showModal=false">
    <div class="modal-info clearfix" ng-click="$event.stopPropagation()">

        <div class='error-text'>this is the content</div>

        <button class="btn form-control" ng-click="showModal=false">OK</button>
    </div>
</div>

到目前为止,这是我的指令:

.directive('modal', function($compile){
    return {
        restrict: 'AE',
        replace: true,
        link: function(scope, element, attrs) {
            scope.content = attrs.content;
        },
        template: '<div class="modal-wrap fade-hide fade-show" ng-show="showModal" ng-click="showModal=false"><div class="modal-info clearfix" ng-click="$event.stopPropagation()">{{content}}<button class="btn form-control" ng-click="showModal=false">OK</button></div></div>',
    };
})

我遇到的问题是 {{content}} 没有被编译。它被呈现为文字 html。

我的模态窗口如下所示:

如何配置我的指令以将content 属性编译为 HTML?

【问题讨论】:

    标签: javascript html angularjs modal-dialog angular-directive


    【解决方案1】:

    在这个示例中,我将引导程序用于我们的模态。

    记住: 在指令中你可以使用 scope 代替 Attr 更好。

    为了在你的指令或任何地方绑定 Html,使用 ng-bind-html 作为这个示例

    index.html

    <modal content="<div class='alert alert-danger'>this is the content</div>"></modal>
    

    modal.html

    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
                <div class="modal-dialog" role="document">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                            <h4 class="modal-title" id="myModalLabel">Modal title</h4>
                        </div>
                        <div class="modal-body" ng-bind-html="content | trust">
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                            <button type="button" class="btn btn-primary">Save changes</button>
                        </div>
                    </div>
                </div>
            </div>
    

    过滤器

    此过滤器可帮助您将 html 绑定为信任代码,我们在 ng-bind-html 属性的 modal.html 中使用它。

    app.filter("trust", ['$sce', function ($sce) {
        return function (htmlCode) {
            return $sce.trustAsHtml(htmlCode);
        }
    }]);
    

    指令

    app.directive('modal', function () {
        return {
            restrict: 'AE',
            templateUrl: "modal.html",
            scope: {
                content: "@"
            },
            link: function (scope, element, attrs) {
                $('#myModal').modal('show');
            }
        };
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-18
      • 1970-01-01
      • 2016-12-13
      • 1970-01-01
      • 1970-01-01
      • 2019-06-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多