【问题标题】:Angular built-in-direcitives not working with Foundation componentsAngular 内置指令不适用于 Foundation 组件
【发布时间】:2018-06-21 23:36:12
【问题描述】:

我使用了很多基础组件,例如 modal 、accordian 等等。在我的 html 中,这些工作正常,但是一旦我使用来自此类 *ngIf 或 *ngFor 的内置指令,这些基础组件就会完全停止工作。下面的例子。

 <p><button class="button" data-open="exampleModal1">Click me for a modal</button></p>
  <h2 class="text-center">Lorem Ipsum</h2>
  <div *ngif="name === 'John'" class="reveal" id="exampleModal1" data-reveal>
      <h1>Awesome. I Have It.</h1>
      <p class="lead">Your couch. It is mine.</p>
      <p>I'm a cool paragraph that lives inside of an even cooler modal. Wins!</p>
      <button class="close-button" data-close aria-label="Close modal" type="button">
        <span aria-hidden="true">&times;</span>
      </button>
 </div>

此模式不起作用,但当我删除 *ngIf 时它运行良好。 这种行为的原因是什么?

此代码是主要问题所在。当我决定用真实数据引入结构指令时,它不起作用。

<div class="app-dashboard-sidebar-inner">
    <div>{{topLabel}}</div>
    <ul class="vertical menu accordion-menu" data-accordion-menu>
    <ng-template ngFor let-asideItem [ngForOf]="asideItems">
      <li>
          <a [routerLinkActive]="['active']" href="#">
            <i class="{{asideItem.icon}}"></i>
            <span class="app-dashboard-sidebar-text">{{asideItem.name}}</span>
          </a>
          <ng-template [ngIf]="asideItem.type === 'sub'">
            <ul class="menu vertial nested">
            <ng-template ngFor let-asideChildren [ngForOf]="asideItem.children">
                <li>
                    <span class="app-dashboard-sidebar-text">{{asideChildren.name}}</span>
                </li>
            </ng-template>
          </ul>
          </ng-template>
      </li>
     </ng-template>
    </ul>
  </div>

所有信息都正确显示,但下拉手风琴不起作用

【问题讨论】:

    标签: angular html zurb-foundation angular-directive


    【解决方案1】:

    只需在ng-template 中使用结构指令即可。

    示例:

    <ng-template *ngif="name === 'John'">
    <div class="reveal" id="exampleModal1" data-reveal>
          <h1>Awesome. I Have It.</h1>
          <p class="lead">Your couch. It is mine.</p>
          <p>I'm a cool paragraph that lives inside of an even cooler modal. Wins!</p>
          <button class="close-button" data-close aria-label="Close modal" type="button">
            <span aria-hidden="true">&times;</span>
          </button>
     </div>
    </ng-template>
    

    【讨论】:

    • 我已经尝试过了,但效果不佳。我正在尝试将结构指令示例 ngFor 与手风琴等组件一起使用。我已经更新了我的问题以包含我用于手风琴的代码
    • 检查您的评论,并且我已经编辑了我的编辑以添加更多信息。请检查
    • 所有信息都正确显示,但下拉手风琴不起作用
    【解决方案2】:

    不能工作的原因是ngIf在条件为false时没有创建DOM(id='exampleModal1'),所以data-open="exampleModal1"找不到目标DOM

    你可以用hidden代替ngIf,我改了这个:

    • 在[隐藏]上使用ngIf的相反逻辑

    <p><button class="button" data-open="exampleModal1">Click me for a modal</button></p>
      <h2 class="text-center">Lorem Ipsum</h2>
      <div [hidden]="name !== 'John'" class="reveal" id="exampleModal1" data-reveal>
          <h1>Awesome. I Have It.</h1>
          <p class="lead">Your couch. It is mine.</p>
          <p>I'm a cool paragraph that lives inside of an even cooler modal. Wins!</p>
          <button class="close-button" data-close aria-label="Close modal" type="button">
            <span aria-hidden="true">&times;</span>
          </button>
     </div>

    【讨论】:

    • 它并不是真的要改变格式,我也在使用 *ngFor 来处理某些组件,但它不能正常工作,因此无法解决问题。我更新了问题以添加在我使用 *ngFor 的地方不起作用的 precies 代码
    • 隐藏只是添加带有 display:none 属性的样式,但 ngif 是从 dom 中删除 html。因此,最好尽可能使用 ngIf 而不是 hidden。
    • ngIf条件为true时,需要一个目标DOM让data-open工作,否则需要在创建DOM时自己重新初始化modal
    • @user3701188 ngFor的源码是来自Http还是任何方式动态创建?
    • @ChunbinLi 我从一个我已经定义了变量的类中获取它,所以我不认为它是数据源的结果,如果有帮助的话我可以发布我的 ngInit
    【解决方案3】:

    我能够让它工作,对于任何可能面临同样问题的人来说,对我有用的解决方案是在 ngAfterViewInit 中调用 $(document).foundation();

    ngAfterViewInit(): void {
    $(document).foundation();
    

    }

    【讨论】:

    • @user3701188 当我引入结构指令时,我的模式或下拉菜单停止工作,我面临同样的问题。我在控制台中收到此错误。 Uncaught TypeError: elem.getBoundingClientRect is not a function at Object.GetDimensions (foundation.js:1117) at Dropdown._setPosition (foundation.js:3628) at Dropdown._setPosition (foundation.js:5807) at Dropdown.open (foundation.js:5940) at Dropdown.toggle (foundation.js:6004) at HTMLDivElement.dispatch (jquery.js:5183) at HTMLDivElement.elemData.handle (jquery.js:4991)
    • 我的任何解决方案。我被添加了ngAfterViewInit(): void { $(document).foundation(); },但仍然没有成功
    猜你喜欢
    • 2021-08-02
    • 2018-06-24
    • 2017-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-27
    • 1970-01-01
    • 2020-05-11
    相关资源
    最近更新 更多