Angular2将值传递给其他类

时间:2015-11-16 13:06:27

标签: javascript ecmascript-6 angular

我正在尝试在我的NG2-app中使用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

1 个答案:

答案 0 :(得分:3)

您应该通过元素

的属性传递数据
<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;
    }