簡體   English   中英

Angular2將值傳遞給其他類

[英]Angular2 passing values to other class

我試圖在我的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

您應該通過元素的屬性傳遞數據

<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;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM