【问题标题】:Two same directives on one controler, dom load一个控制器上有两个相同的指令,dom load
【发布时间】:2017-02-06 17:01:09
【问题描述】:

你好,我有控制器模板,我在其中声明了两个用于横幅的指令。 所以我有 home.html 和

<div data-banner="{typeName: '1'}">
<div data-banner="{typeName: '2'}">

function bannerDirective() {
    return {
        restrict: 'A',
        templateUrl: 'banner/banner.html',
        controllerAs: 'vm',
        bindToController: true,
        controller: bannerController,
        scope: {
            bannerOptions: '=banner'
        },
        link: function(scope) {
            scope.$watch('bannerOptions', function() {
                $(".owl-carousel").owlCarousel({
                    loop: true,
                    autoWidth: true,
                    autoPlay: true,
                    pagination: false,
                    items: 1
                });
            });
        }
    };
}

我需要在这两个指令上使用$(".owl-carousel").owlCarousel 来显示路线更改两个横幅。而且我不知道如何使它工作。

【问题讨论】:

  • link 传递一个元素。
  • 在控制器中有从服务器获取横幅的方法。
  • 也有可用的模块见github.com/jonahbron/angular-owl-carousel。在重新发明轮子之前检查模块
  • 为此我讨厌 Angular,我不知道插件的存在,我浪费时间。谢谢,我想它会解决我的问题。
  • google 搜索不难

标签: javascript angularjs owl-carousel


【解决方案1】:

您必须在从指令的 link 函数中获得的特定元素上调用 owlcarousel

link: function(scope, element) {
  scope.$watch('bannerOptions', function() {
    element.owlCarousel({
      loop: true,
      autoWidth: true,
      autoPlay: true,
      pagination: false,
      items: 1
    });
  });
}

【讨论】:

  • link: function(scope, element) { element.children().owlCarousel({ loop: true, autoWidth: true, autoPlay: true, pagination: false, items: 1 }); } 这不起作用。
  • 为什么是children?你能告诉我这些divs的html内容吗?
  • 也许只使用现有的指令,例如github.com/jonahbron/angular-owl-carousel
猜你喜欢
相关资源
最近更新 更多
热门标签