【问题标题】:Call a method on click on a node on cytoscape graph单击 cytoscape 图上的节点调用方法
【发布时间】:2021-06-14 19:15:36
【问题描述】:

我试图通过传入节点的 id 来调用方法,但它一直说该方法不是函数。

这里是 stackblitz 链接:https://stackblitz.com/edit/cytoscape-call-method?file=src/app/app.component.ts

您可以单击任何节点并查看控制台错误。它说 this.showId 不是函数。

一些代码

 this.cy.on('click', 'node', function(evt) {
      console.log('clicked ' + this.id());
      this.showId(this.id());
    });

我也尝试过使用tap,但仍然出现同样的错误。

【问题讨论】:

  • 试试evt.target.id()
  • 你应该阅读 cytoscape.js events。上面的评论工作得很好,你不需要一个函数(showId)。
  • @StephanT。这只是一个例子。我想传递 id 并调用服务以获取更多数据。问题是事件无法识别类中的函数。它必须在类之外,这意味着所有服务在该类之外都不可用
  • @canbax 问题不在于获取 id,而在于调用我提到的方法。这只是一个例子,我需要 id 来调用其他东西或调用注入到类的构造函数中的服务

标签: cytoscape.js cytoscape cytoscape-web


【解决方案1】:

嗯,我想我明白你的意思了。这是关于 JS 中的范围。试试下面

this.cy.on('click', 'node', (evt) => {
  console.log('clicked ' + this.id());
  this.showId(this.id());
});

以下

this.cy.on('click', 'node', function (evt) {
  console.log('clicked ' + this.id());
  this.showId(this.id());
}.bind(this));

两者其实是一样的

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-19
    相关资源
    最近更新 更多