为什么当其容器上有 ng

编程入门 行业动态 更新时间:2024-10-21 09:35:45
本文介绍了为什么当其容器上有 ng-show 时图表更新会失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我有一个编译指令,其中包含一个 angular-charts.js 指令.

我注意到,当该图表的容器具有 ng-showng-hide 作为属性时,图表不会更新——它只是不更新根本不显示.

这里有一个 plunker 演示了这一点(参见 listeningComponentscripts.js 指令中)

解决方案

这里的问题不在于 ng-showng-hide 属性.

这背后的根本原因是 DOM 操作.

这里首先执行 ng-show 条件,然后加载指令,因此在加载指令后,您的变量值在这里发生变化.

所以尝试使用 ng-if 代替 ng-show.它将解决您的问题.

更改您的 listner.html 模板.

listeningComponent 指令

<div class="listener"><p>{{听众文本}}</p><div class="bar-chart-box" ng-if="loading === false">带有 ng-show
的条形图<canvas class="chart chart-bar"数据=图表数据.数据"标签=图表数据.标签"series="chartData.series"></画布>

<小时><div class="bar-chart-box">没有 ng-show 的条形图
<canvas class="chart chart-bar"数据=图表数据.数据"标签=图表数据.标签"series="chartData.series"></画布>

I have a compiled directive that contains an angular-charts.js directive in it.

I've noticed that when the container of that chart has ng-show or ng-hide as an attribute, the chart wont update -- it just doesn't show at all.

Here is a plunker that demonstrates this (see listeningComponent in scripts.js directive)

解决方案

Here the problem is not with the ng-show and ng-hide attribute.

The root cause behind this is is DOM manupulation.

Here ng-show condition is executing first and directive is loading after that so your variable value is changing here after directive loading.

So try with ng-if instead ng-show. It will solve your problem.

Change your listner.html template.

<h4>listeningComponent Directive</h4>
<div class="listener">  
  <p>
    {{listenertext}}
  </p>
  <div class="bar-chart-box" ng-if="loading === false">
    Bar Graph with ng-show<br>
    <canvas class="chart chart-bar"
      data="chartData.data"
      labels="chartData.labels"
      series="chartData.series">
    </canvas>
  </div>
  <hr>
  <div class="bar-chart-box">
    Bar Graph without ng-show<br>
    <canvas class="chart chart-bar"
      data="chartData.data"
      labels="chartData.labels"
      series="chartData.series">
    </canvas>
  </div>  
</div>

这篇关于为什么当其容器上有 ng-show 时图表更新会失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-22 11:35:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1023565.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:上有   容器   ng

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!