我正在使用Angular 4应用程序中的d3.js
。我正试图调用一个函数,但它会引发一个错误,如ERROR TypeError: this.highlightNodes is not a function
。this.someFunction(params)不是Angular 4中的函数
下面的代码,我工作:
import {Component, OnInit} from '@angular/core';
declare let d3:any;
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
ngOnInit() {
this.networkGraph();
}
networkGraph() {
d3.netJsonGraph("../assets/json/network.json",
{
el: "#randomDiv",
onClickNode: function (url, opts) {
//Here's the error being thrown
this.highlightNodes(url, "nodes", true);
}
}
);
}
highlightNodes(url, type, initial) {
console.log("Its working");
}
}
使用bind(this)
是没有帮助,因为它是局部结合。请帮我解决这个问题,如何以正确的方式调用函数。
要调用一个单击处理这不是一个箭头函数中的方法。这将改变上下文。尝试'nClickNode:(url,opts)=> {}' – Rajesh
必须有'这个'的双关语(双关语意思:) :) – abhishekkannojia