Unable to get access to private variable inside class function

Viewed 64

I am working in an angular project and designing d3 pie charts. Everything works fine. But my problem is that I am not able to access class private variable inside a function.

2 Answers

Scope issue. Change

.attr('y', function (d, i) { return (this.legendHeight * (i + 1))})

To

.attr('y', (d, i) => { return (this.legendHeight * (i + 1))})

One of many docs

The problem you are facing can be explaned: within your function (d, i) { return (this.legendHeight * (i + 1))} it so happens to be that this referes to another context than you might expect. try changing it to an Arrow notation, like (d,i) => this.legendHeight * (i + 1) to solve your issue.

If you cannot use this notation you will need to make a local reference to your value of legendHeight

Related