【发布时间】:2016-02-24 07:43:38
【问题描述】:
我正在尝试构建绘制图表的 angular2 组件(使用 jquery plot)
import {Component, ElementRef, Input, OnChanges} from 'angular2/core';
@Component({
selector: 'flot',
template: `<div>loading</div>`
})
export class FlotCmp implements OnChanges{
private width = '100%';
private height = 220;
static chosenInitialized = false;
@Input() private options: any;
@Input() private dataset:any;
@Input() private width:string;
@Input() private height:string;
constructor(public el: ElementRef) {}
ngOnChanges() {
if(!FlotCmp.chosenInitialized) {
let plotArea = $(this.el.nativeElement).find('div').empty();
plotArea.css({
width: this.width,
height: this.height
});
$.plot( plotArea, this.dataset, this.options);
FlotCmp.chosenInitialized = true;
}
}
}
组件获取图表“数据”属性作为输入参数:
<flot [options]="splineOptions" [dataset]="dataset" height="250px" width="100%"></flot>
到目前为止,只要“数据集”是静态变量,我就设法让它工作。
this.dataset = [{label: "line1",color:"blue",data:[[1, 130], [3, 80], [4, 160], [5, 159], [12, 350]]}];
我的问题是当数据作为承诺时让它发挥作用:
export class App implements OnInit {
private dataset:any;
public entries;
getEntries() {
this._flotService.getFlotEntries().then(
entries => this.dataset[0].data = entries,
error => this.errorMessage = <any>error);
}
ngOnInit() {
this.getEntries()
}
constructor(private _flotService:FlotService) {
this.name = 'Angular2'
this.splineOptions = {
series: {
lines: { show: true },
points: {
radius: 3,
show: true
}
}
};
this.dataset = [{label: "line1",color:"blue",data:null]}];
}
}
由于某种原因,数据更改无法投射到“浮动”组件
这里是plunk的链接
请帮忙
【问题讨论】: