首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用@ViewChildren处理高级图表

使用@ViewChildren处理高级图表
EN

Stack Overflow用户
提问于 2017-06-09 12:13:03
回答 1查看 683关注 0票数 1

关于如何在角2:https://angular2highcomponents.azurewebsites.net/LineCharts/BasicLine中使用高图(基本线图),有一个例子

我想知道如何动态地实现这一点,而不知道我将绘制多少个图表。下面是我尝试过的(但失败了):

在我的component.ts:

代码语言:javascript
复制
@ViewChildren('chart') components: QueryList<any>;
Highcharts.chart(this.components.toArray()[0].nativeElement, this.lineConfig); //with a legit lineConfig, of course

在我的component.html:

代码语言:javascript
复制
<div *ngFor="let widget of widgets" class="col-xs-12 col-sm-6 placeholder2">
        <div #chart style="width:100%; height:300px;"></div>
      </div>

我知道这个错误:

TypeError:无法读取未定义的属性“nativeElement”

我得到这个错误是因为组件数组不是初始化的,而是使用viewChild的,这不是一个问题(如您在示例中所看到的)。

提前谢谢。

EN

回答 1

Stack Overflow用户

发布于 2017-06-09 12:22:52

你可以尝试几种方法,我会把它们都分组:

代码语言:javascript
复制
import { Component, OnInit, AfterViewInit, AfterViewChecked } from '@angular/core';

export class YourClass, implements OnInit, AfterViewInit, AfterViewChecked {
    ngOnInit() {
        Highcharts.chart(this.components.toArray()[0].nativeElement, this.lineConfig); //with a legit lineConfig, of course
    }
    ngAfterViewInit() {
        Highcharts.chart(this.components.toArray()[0].nativeElement, this.lineConfig); //with a legit lineConfig, of course
    }
    ngAfterViewChecked() {
        Highcharts.chart(this.components.toArray()[0].nativeElement, this.lineConfig); //with a legit lineConfig, of course
    }
}

尝试使用AfterViewChecked first, it should work.

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44457558

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档