javascript - Angular2 将值传递给其他类

标签 javascript ecmascript-6 angular

我正在尝试在我的 NG2 应用程序中使用 D3 图表,因为我已经启动并运行了“图表”和硬编码数据。我正在寻找一种能够将数据从另一个组件传递到图表的方法。这是“父”组件,其中包含显示图形的指令:

@Component({
  selector: "homeTest",
   properties: [ 'data' ]
})
@View({
  directives: [CORE_DIRECTIVES, BarGraph],  
    template: `
  <h1 class="title">Angular 2</h1>
  <bar-graph>
  </bar-graph> 
  `

})
export class HomeTest {

  meter: any;
  consumption: any;
  elementRef: any;
  constructor() {}

我从这个组件中获取我想在图表中使用的数据。假设它看起来像这样:

PassThisArrayToBarChart(){
var dataset = [ 
    { key: 0, value: 5 },
    { key: 1, value: 10 }
     ];


 //Pass this dataSet to Barchart somehow?
}

这是我们拥有需要数据集的 D3 图表的类:

@Directive({
  selector:   'bar-graph',
})
class BarGraph {
  data: Array<number>;
  divs: any;
  dataset: any;
  constructor( 
    @Inject(ElementRef) elementRef: ElementRef,
    @Attribute('width') width: string,
    @Attribute('height') height: string) {

    var w = 600;
    var h = 250;

var dataset = [ 
    { key: 0, value: 5 },
    { key: 1, value: 10 }
     ];

...

正如您在此处看到的,我有一个可以正常工作的硬编码数据集,但如何将其替换为从 HomeTest 传递的数据集?

如果需要,这里是 BarGraph 的其余部分:

var xScale = d3.scale.ordinal()
                .domain(d3.range(dataset.length))
                .rangeRoundBands([0, w], 0.05); 

var yScale = d3.scale.linear()
                .domain([0, d3.max(dataset, function(d) {return d.value;})])
                .range([0, h]);

var key = function(d) {
    return d.key;
};

//Create SVG element
var svg = d3.select("bar-graph")
            .append("svg")
            .attr("width", w)
            .attr("height", h);

//Create bars
svg.selectAll("rect")
   .data(dataset, key)
   .enter()
   .append("rect")
   .attr("x", function(d, i) {
        return xScale(i);
   })
   .attr("y", function(d) {
        return h - yScale(d.value);
   })
   .attr("width", xScale.rangeBand())
   .attr("height", function(d) {
        return yScale(d.value);
   })
   .attr("fill", function(d) {
        return "rgb(0, 0, " + (d.value * 10) + ")";
   })



//Create labels
svg.selectAll("text")
   .data(dataset, key)
   .enter()
   .append("text")
   .text(function(d) {
        return d.value;
   })
   .attr("text-anchor", "middle")
   .attr("x", function(d, i) {
        return xScale(i) + xScale.rangeBand() / 2;
   })
   .attr("y", function(d) {
        return h - yScale(d.value) + 14;
   })
   .attr("font-family", "sans-serif") 
   .attr("font-size", "11px")
   .attr("fill", "white");

  }

更新:

@Component({
  selector: "homeTest",
  properties: [ 'data' ]
})
@View({
  directives: [CORE_DIRECTIVES, BarGraph],  

    template: `
  <h1 class="title">Angular 2 + d3</h1>
  <bar-graph [graphdata]="dataset">
  </bar-graph>
  `

})

    export class HomeTest {
      dataset: Array<Object> = [ 
        { key: 0, value: 5 },
        { key: 1, value: 10 }
      ];

      constructor() {}

条形图:

@Directive({
  selector:   'bar-graph',

})
export class BarGraph {
  @Input() graphdata;
  data: Array<number>;
  divs: any;
  dataset: any;
  constructor( 
    @Inject(ElementRef) elementRef: ElementRef,
    @Attribute('width') width: string,
    @Attribute('height') height: string) {

    var w = 600;
    var h = 250;

var dataset = this.graphdata; //graphData undefined

最佳答案

你应该通过元素的属性传递数据

<bar-graph [graphdata]="dataset">

所以你在父类中需要这个

export class HomeTest {
  dataset: Array<Object> = [ 
    { key: 0, value: 5 },
    { key: 1, value: 10 }
  ];

然后在组件本身中获取它

class BarGraph {
    @Input() graphdata;
    constructor() {}
    afterViewInit() {
      var dataset = this.graphdata;
    }

关于javascript - Angular2 将值传递给其他类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33736145/

相关文章:

javascript - 如何将当前范围传递给函数引用?

javascript - 在 JavaScript ES6 类中无限期调用 "set function()"

android - 单击 service-worker ng7 + android 处理的推送通知时打开 PWA

javascript - 如何在 Angular 中的服务器上仅上传一个文件?

javascript - 将鼠标悬停在侧边栏中的项目上时,gmaps4rails 会更改标记颜色

javascript - 是否可以在 CkEditor 或 TinyMce 中从编辑器中拆分工具栏或菜单栏?

javascript - 使用 .prependTo() 时奇怪的边距

javascript - 使用 JavaScript 和 JSP 重定向到另一个页面

javascript - 未找到从 mobx 导入可观察的内容或其他内容

node.js - 如何连接到 Angular ,nodejs http ://localhost:4200 in google chrome