javascript - 访问函数内的变量::TypeScript & Angular2

标签 javascript angular typescript scope

我有一个 c3.js 库,它在我的 angular2 文件中绘制图表。绘图脚本位于函数内部:

private draw() {

 let chart = c3.generate({
  bindto: '#chart',
  data: {
    columns: [
      ['data1', 30, 200, 100, 400, 150, 250, 130, 50, 20, 10, 40, 15, 25, 390],
      ['data2', 50, 20, 10, 40, 15, 25, 542, 30, 200, 100, 333, 150, 250]
    ],
    type: "line"
  }   

});

}

c3.js 库提供了通过使用以下函数将图表类型从 line 更改为 spline 的可能性:

chart.transform('样条线');

但不幸的是,我无法访问此 chart 变量,因为它位于 draw() 函数内部。

我想将此 chart.transform('spline'); 函数放在按钮内,以便用户动态更改它。

~我需要一些方法来使其发挥作用,非常感谢任何帮助!

最佳答案

chart:any;

private draw() {

 this.chart = c3.generate({
  bindto: '#chart',
  data: {
    columns: [
      ['data1', 30, 200, 100, 400, 150, 250, 130, 50, 20, 10, 40, 15, 25, 390],
      ['data2', 50, 20, 10, 40, 15, 25, 542, 30, 200, 100, 333, 150, 250]
    ],
    type: "line"
  }   

});

您可以在任何地方使用this.chart

关于javascript - 访问函数内的变量::TypeScript & Angular2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41163957/

相关文章:

node.js - 错误 TS2307 : Cannot find module 'bluebird'

javascript - 具有相同高度的多个div,Jquery/Javascript

javascript - jQuery - "blind"中的选项 "setInterval"仍在运行

angular - Imgur 嵌入在 Angular Web 应用程序中不显示图像

typescript - 如何用 typescript 接口(interface)描述 mobx-state-tree 的模型?

typescript - 在 TypeScript 中键入表示 "this very class"

typescript - 如何在 TypeScript 中指示路径不可访问

javascript - 搜索下拉菜单

javascript - 如何从 JavaScript 运行 Razor 函数

angular - 改变输入值 Angular 5