2016-12-16 13 views
1

私はドラッグ&切妻とドラッグが正常に動作のいくつかのバブルを持っています。 drag-to xとyにアクセスする場合、私は今何をする必要があるのですか?それらの値を使用して、自分のコンポーネント上にあるデータセットで値を計算します。私が遭遇した問題はドラッグエンド機能です。これはドラッグされたオブジェクトを指しています。実際の親スコープにアクセスできるようになりました。角度2 D3ドラッグ機能のスコープの問題私はd3.jsのV4.4と2</p> <p>角度を使用しています

これはバブルを追加した方法です。

showPeopleBubble() { 
     let self = this; 
     console.log('run') 
     this.bubblePeople = this.canvas.selectAll('.bubblePeople') 
      .data(this.nodes) 
      .enter().append('g') 
      .attr('class','.bubblePeople').attr('id',function(d){return 'i'+d.index}) 
      .attr('transform',"translate(100,100)") 
      .call(D3['drag']().on("start", self.dragstarted) 
       .on("drag", self.dragged) 
       .on("end", self.dragended)); 

      this.bubblePeople.append("title").text(function(d){return d.name + ' ('+d.index+')'}) 

      this.bubblePeople.append('circle') 
       .attr('r',30) 
       .attr('fill','blue') 
       .attr('fill-opacity',.3) 
       .attr("text-anchor","middle") 

      this.bubblePeople.append('text').text(function(d){return d.name.substring(0,30/3)}); 

    } 

dragended(d) { 
    // this in here is the bubble that i'm dragging 
//i need to access the parent level. 


} 

答えて

1

は手動で次のようにコールバックを使用することができます。

showPeopleBubble() { 
     let self = this; 
     console.log('run') 
     this.bubblePeople = this.canvas.selectAll('.bubblePeople') 
      .data(this.nodes) 
      .enter().append('g') 
      .attr('class','.bubblePeople').attr('id',function(d){return 'i'+d.index}) 
      .attr('transform',"translate(100,100)") 
      .call(D3['drag']().on("start", self.dragstarted) 
       .on("drag", self.dragged) 
       .on("end", function(d){ 
        return self.dragended(d, this, self); 
       })); 

      this.bubblePeople.append("title").text(function(d){return d.name + ' ('+d.index+')'}) 

      this.bubblePeople.append('circle') 
       .attr('r',30) 
       .attr('fill','blue') 
       .attr('fill-opacity',.3) 
       .attr("text-anchor","middle") 

      this.bubblePeople.append('text').text(function(d){return d.name.substring(0,30/3)}); 

    } 

dragended(d, innerThis, globalThis) { 
    // this in here is the bubble that i'm dragging 
//i need to access the parent level. 
    // globalThis.someFunction(); <-- will call the global someFunction() method 

} 

someFunction(){} 

私もそう、あなたがグローバルスコープでdragended(d)関数の中にそれを失うことはありません機能にthisを入れています。

+0

おかげで、これを試して取得していない、私は、(この) – chungtinhlakho

+0

問題はない@chungtinhlakho .bind使用して終了しますが、メソッドにこの世界をバインドする場合は、イベント 'this'を失うことになることに注意してください。これはこの問題とは関係ないかもしれませんが、 'mouseclick'のようなイベントには関係します。ちょうどあなたに知らせるために。 – echonax

+1

おかげで、ありがとう、私はあなたを試して、完全にうまく動作し、より柔軟性も提供します。どうもありがとう。 – chungtinhlakho

0

.bindを使用して終了します。これは必要なデータまたはデータをバインドできます。

.call(D3['drag']().on("start", self.dragstarted) 
       .on("drag", self.dragged) 
       .on("end", self.dragended.bind(this))); 
関連する問題