【问题标题】:AngularUI Bootstrap Carousel not working properly when binding HTML content with $compile使用 $compile 绑定 HTML 内容时,AngularUI Bootstrap Carousel 无法正常工作
【发布时间】:2017-06-28 17:06:21
【问题描述】:

AngularUI 引导版本 ^2.4.22

AngularJS 版本 1.6.4

Angular Sanitize 版本 ^1.6.1

我在使用 AngularUI Bootstrap 的 Carousel 插件时遇到问题。在我的场景中,我需要读取一个包含一些模板路径的外部文件,并将它们中的每一个加载为幻灯片。请参见下面的示例:

index.html(指令被调用的地方)

     <body id="body" ng-app="homePage">
        <div id="miolo">
            <div example-directive class="ng-hide"></div>
            <div banner-rotativo></div>
            <div id="menu-footer"></div>
        </div>
     </body>

banner-rotativo.directive.js - 基本上,在这个指令中,我有一个遵循AngularUI Bootstrap's demo 结构的模板,并且我将响应数据绑定到$scope.slides 数组。当我将htmlContent 属性推入$compile(objResponseInner)($scope) 结果时,轮播行为可以正常工作,但它会将 [[object HTMLDivElement]] 之类的东西呈现为一个项目。

angular.module('homePage')
.directive('bannerRotativo', ['$compile', '$http', 'moduleUrl', '$templateRequest', function ($compile, $http, moduleUrl, $templateRequest) {
    return {
        template:   '<div style="height: 305px" ng-controller="bannerHomeController" class="" >\
                        <div uib-carousel active="active" interval="myInterval" no-wrap="noWrapSlides">\
                            <div uib-slide ng-repeat="slide in slides track by slide.id" index="slide.id">\
                                <div ng-bind-html="slide.htmlContent">\
                                </div>\
                            </div>\
                        </div>\
                    </div>',

        link: function (scope, element, attributes, controller) {

            //Carousel
            scope.myInterval = 5000;
            scope.noWrapSlides = false;
            scope.active = 0;
            scope.slides = [];
            var intCurrentIndex = 0;

            $http({
                method: 'GET',
                url: moduleUrl.getUrl('homepage', '../config/banner-rotativo.conf')
            }).then(function success(objResponse, status, headers, config) {
                var objData = objResponse.data;
                if (objData.slides) {
                    angular.forEach(objData.slides, function (objItem, strObjectName) {
                        var strTemplatePath = moduleUrl.getUrl('homepage', '..' + objItem.caminho);
                        if (strTemplatePath) {
                            $templateRequest(strTemplatePath).then(function success(objResponseInner) {
                                var objContent = $compile(objResponseInner)(scope);
                                scope.slides.push({
                                    htmlContent: objContent,
                                    id: intCurrentIndex++
                                });
                            });
                        }
                    });
                }
            });
        }
    }


}]);

banner-rotativo.conf

{
"slides": {
    "banner-ex-one": {
        "titulo": "exone",
        "caminho-imagem": "assets/one.jpg",
        "caminho": "/html/components/banner-rotativo/banner-ex-one.view.html"
    },
    "banner-ex-two" : {
        "titulo": "extwo",
        "caminho-imagem": "assets/two.jpg",
        "caminho": "/html/components/banner-rotativo/banner-ex-two.view.html"
    },
    "banner-rav" : {
        "title": "rav",
        "caminho-imagem": "assets/rav.jpg",
        "caminho": "/html/components/banner-rotativo/banner-rav.view.html"
    },
    "banner-aviso" : {
        "title": "Quadro de comunicações 1",
        "caminho-imagem": "assets/aviso.jpg",
        "caminho": "/html/components/banner-rotativo/banner-aviso.view.html"
    },
    "banner-custom" : {
        "title": "Quadro de comunicações 2",
        "caminho-imagem": "assets/custom.jpg",
        "caminho": "/html/components/banner-rotativo/banner-custom.view.html"
    }
  }
}

加载的模板示例:

<div id="frameOne" ng-controller="slideOneController" class="varejo-clique-promocao-one" title="Conteúdo Varejo - Quadro One">
<div class="item">
    <div id="dados">

        <!-- Imagem banner one -->
        <img id="one" ng-click="enviarFormOne()" class="one" alt="" ng-if="1==1" ng-src="caminhoImagem"
        />
        <!-- End imagem banner -->
        <span ng-if="hasText">{{bannerText}}</span>
    </div>
    <div id="excecao" class="excecao" ng-if="typeof(one.excecao) !== 'undefined'">
        DEU EXCECAO
    </div>
    <div class="carousel-title" id="tituloOne" ng-if="1==1" title="{{bannerTitle}}">
        {{bannerTitle}}
    </div>
</div>

一些重点:

  • 我正在使用$compile,因为我注入的模板也有控制器。如果我不使用它,我的控制器不会被处理。当我简单地插入没有$compile 的 HTML 时,它呈现正常。

怎么了?

【问题讨论】:

    标签: javascript angularjs angular-ui-bootstrap angular-ui


    【解决方案1】:

    问题在于 ng-bind-html 需要 html 字符串,而您将 $compile 的结果提供给它实际上是一个元素对象。您可以从已编译的元素中获取 HTML 字符串并传递它,但您可能会遇到各种麻烦。

    但您实际上可以通过使用ngInclude 而不是 ng-bind-html 来跳过 $compile 的麻烦。它将为您处理模板请求和编译。

    所以不要请求和编译strTemplatePath,而是将其存储到幻灯片列表中:

    var strTemplatePath = moduleUrl.getUrl('homepage', '..' + objItem.caminho);
    if (strTemplatePath) {
        scope.slides.push({
            htmlUrl: strTemplatePath,
            id: intCurrentIndex++
        });
    }
    

    然后在模板中使用它:

    <div uib-slide ng-repeat="slide in slides track by slide.id" index="slide.id">
        <div ng-include="slide.htmlUrl"></div>
    </div>
    

    这是一个有点工作的小提琴:jsfiddle.net,虽然我不得不在这里和那里填写一些空白。

    【讨论】:

    • 该死的家伙,你是个传奇!我是 Angular 的新手,为了解决这个问题,我在墙上敲了 3 天。谢谢!!!
    猜你喜欢
    • 2014-07-05
    • 2021-04-07
    • 2015-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多