【问题标题】:Angular changedetection ngFor with primeng OverlayComponent带有primeng OverlayComponent的角度变化检测ngFor
【发布时间】:2019-11-22 22:54:28
【问题描述】:

我正在制作一个 Angular 应用程序,一切似乎都运行良好。 但我质疑表现。

所以我尝试实现 changedetectionStrategy.OnPush。

我使用了 primeng 的工具提示和覆盖面板 https://www.primefaces.org/primeng/#/overlaypanel

此列表中的每次单击都会触发 getTooltip 事件。这当然是不需要的。 click 事件触发 changedetection 更改。

<div #actualTarget
     *ngFor="let event of day.TimeEvents"
     class="event"
     pTooltip="{{getTooltip(event)}}"
     tooltipPosition="bottom"
     showDelay="200"
     (click)="openOverlay(op, $event, actualTarget)">

我尝试使用带有自定义指令的 ngZone 在外部运行 click 命令,但这不会显示 overlayPanel,因为它不会触发 changedetection https://medium.com/@krzysztof.grzybek89/how-runoutsideangular-might-reduce-change-detection-calls-in-your-app-6b4dab6e374d

是否可以轻松解决,以便显示覆盖面板,但不会再次为每个 div 元素调用工具提示方法?

【问题讨论】:

    标签: angular


    【解决方案1】:

    您的问题不是更改检测,而是模板内的函数调用。 它将被多次调用pTooltip="{{getTooltip(event)}}"。可能更好地将方法移动到管道pTooltip="{{event | getTooltip}}"

    您可以了解更多here

    【讨论】:

    • 我会试试的,谢谢。 div 元素上的 [ngClass] 怎么样?这也会在每次点击点击事件时触发
    • 也许这个答案stackoverflow.com/questions/56335963/… 会有所帮助。或者尽可能简单地使用 [class] 绑定而不在模板中调用函数
    猜你喜欢
    • 2021-08-07
    • 2021-06-17
    • 2018-10-24
    • 1970-01-01
    • 2018-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多