al3*_*350 5 highcharts angular2-highcharts angular
我使用angular2-highcharts,我想在本地函数中调用组件方法,但我不知道它是如何可能的.
你可以帮帮我吗 ?一个吸引人的例子:http://plnkr.co/edit/gOLGytp9PZXiXvv2wv1t?p = preview
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { NgModule, Component } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { ChartModule } from 'angular2-highcharts';
@Component({
selector: 'my-app',
styles: [`
chart {
display: block;
}
`],
template: `<chart [options]="options"></chart>`
})
class AppComponent {
constructor() {
this.options = {
chart: {
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false,
type: 'pie'
},
title : { text : 'simple chart' },
plotOptions: {
series: {
turboThreshold:3000,
cursor: 'pointer',
point: {
events: {
click: function() {
console.log(this);
// I want to call a component method here
}
}
}
}
},
series: [{
name: 'Brands',
colorByPoint: true,
data: [{
name: 'Microsoft Internet Explorer',
y: 56.33
}, {
name: 'Chrome',
y: 24.03,
sliced: true,
selected: true
}, {
name: 'Firefox',
y: 10.38
}, {
name: 'Safari',
y: 4.77
}, {
name: 'Opera',
y: 0.91
}, {
name: 'Proprietary or Undetectable',
y: 0.2
}]
}]
};
}
options: Object;
methodToCall(){
console.log("Method called");
}
}
@NgModule({
imports: [BrowserModule, ChartModule.forRoot(require('highcharts'))],
declarations: [AppComponent],
bootstrap: [AppComponent]
})
class AppModule { }
platformBrowserDynamic().bootstrapModule(AppModule);
Run Code Online (Sandbox Code Playgroud)
您只需要将匿名函数替换为箭头函数或将其绑定到组件:
point: {
events: {
click: () => {
console.log(this);
// I want to call a component method here
}
}
}
Run Code Online (Sandbox Code Playgroud)
或者
point: {
events: {
click: (function(){
console.log(this);
// I want to call a component method here
}).bind(this)
}
}
Run Code Online (Sandbox Code Playgroud)
甚至 :
point: {
events: {
click: this.myComponentMethod.bind(this)
}
}
Run Code Online (Sandbox Code Playgroud)
要访问click事件返回的信息并且还可以访问组件方法,您可以执行以下操作:
plotOptions: {
series: {
turboThreshold:3000,
cursor: 'pointer',
point: {
events: {
click: function(e){
const p = e.point
this.myComponentMethod(p.category,p.series.name);
}.bind(this)
}
}
}
},
Run Code Online (Sandbox Code Playgroud)
我希望这将有所帮助.
归档时间: |
|
查看次数: |
2997 次 |
最近记录: |