我正在尝试使用ng2-chart动态创建图表,我从角度2服务获取信息,当我仅更改图表的标签时它工作,当我更改数据时它只能工作,但当我更改两者时,只更新数据在图表中.对这种奇怪的行为有任何解释.
我的模板:
<canvas baseChart height="130" width="180"
[data]="doughnutChartData"
[labels]="doughnutChartLabels"
[chartType]="doughnutChartType"
(chartHover)="chartHovered($event)"
(chartClick)="chartClicked($event)">
</canvas>
Run Code Online (Sandbox Code Playgroud)
我的课 :
export class PlDoughnutComponent implements OnInit {
constructor(private homeService: TileServiceService) { }
ngOnInit() {
this.updatePLdoughnut();
}
public util : UtilService = new UtilService();
public doughnutChartLabels:string[] = ['Download Sales'];
public doughnutChartData:number[] = [0,0,100];
public doughnutChartType:string = 'doughnut';
public updatePLdoughnut(){
this.homeService.getTile().
then(res => {
this.doughnutChartLabels = res.PLtypes;
this.doughnutChartData = this.util.objectToIntArray(res.PLByTypes);
})
}
}
Run Code Online (Sandbox Code Playgroud) 我是 Angular2 的新手,我有两个服务DefaultService并且SpecialService应该根据组件状态交替使用我不知道我是否可以将一个单例注入到我的组件中,该单例GeneralService以某种方式表现得像 DefaultService或SpecialService通过更改其实现。
我还有另一个想法是将这两个服务注入generalService并将它们的方法委托给适当的服务。
感谢告诉我如果可行,我如何实现第一个想法,或者建议我其他人。
@Component({
selector: 'my-app',
templateUrl : './content.component.html'
})
export class AppComponent {
constructor(generalService : GenralService,injector : Injectot){}
@ViewChild(MySon)
private mySon : MySon;
state : State;
onChangeState(newState : State):void{
if(state = special)
//this.generalService = this.injector.get('SpecialService');
myson.updateplease();
}
}
Run Code Online (Sandbox Code Playgroud)
假设SpecialService和DefaultService扩展GeneralService