切换标签后,离子段不显示数据

时间:2017-06-17 14:07:29

标签: javascript angularjs cordova ionic-framework ionic2

segments切换后,我无法显示图表。 我目前有2个段,索引和编辑。在索引上,我成功显示了Chart.js的图表。

图表代码:

diagram = function() {
    var finished = 0;
    var pending = 0;

    for (var i = 0; i < this.goal.activities.length; i++) {
      var current = this.goal.activities[i];
      if (current.completed == true) { finished++ } else { pending++ }
    }
    this.piChart = new Chart(this.piCanvas.nativeElement, {
      type: 'doughnut',
      data: {
          labels: ["finished activities", "pending activities"],
          datasets: [{
            label: '# of Votes',
            data: [
              finished
              ,
              pending
            ],
            backgroundColor: [
              'rgba(255, 99, 132, 0.2)',
              'rgba(54, 162, 235, 0.2)'
            ],
            hoverBackgroundColor: [
              "#FF6384",
              "#36A2EB"
            ]
          }]
      }
    })
 }

并调用该函数:

ionViewDidEnter() {
  this.diagram();
}

现在在页面上它是初始视图,图表加载成功并产生一个美丽的甜甜圈。问题是在我单击一个段然后返回到初始页面(带有图表)之后。现在图表已经消失了。我仍然在HTML中看到它,但它不会在视图中呈现。

视图中的代码:

<ion-header>
  <ion-navbar color="primary">
    <ion-title>{{ goal.title }}</ion-title>
  </ion-navbar>
</ion-header>

<ion-content>
  <ion-segment [(ngModel)]="goalTab">
    <ion-segment-button value="progress">
      Progress
    </ion-segment-button>
    <ion-segment-button value="activities">
      Actitivies
    </ion-segment-button>
  </ion-segment>

  <div [ngSwitch]="goalTab">
    <ion-card *ngSwitchCase="'progress'">
      <ion-card-header>
        Overview of activities for: {{ goal.title }}
      </ion-card-header>
      <ion-card-content>
        <canvas #piCanvas></canvas>
      </ion-card-content>
    </ion-card>

    <ion-list *ngSwitchCase="'activities'">
      <ion-item>
        <ion-card-header>
          Something other segment
        </ion-card-header>
      </ion-item>
    </ion-list>
  </div>
</ion-content>

希望有人能够找到问题并帮助我,谢谢!

更新

Canvas with it的图表不在HTML中。在它的初始加载我看到以下(这是正确的): enter image description here

切换到另一个段并返回到应该呈现图表的段(不正确)之后: enter image description here

因此,在从段切换后,图表未正确初始化。

1 个答案:

答案 0 :(得分:0)

在图表的离子卡中试试这个:

   <ion-card [style.display]="goalTab == 'progress' ? 'block' : 'none'">
      <ion-card-header>
         Overview of activities for: {{ goal.title }}
      </ion-card-header> 
      <ion-card-content> 
         <canvas #piCanvas></canvas>
      </ion-card-content> 
   </ion-card>