Exception when using with Angular ngFor/ngIf

We are considering purchasing a license for dhx-spreadsheet. However we have run into a problem when integrating with our application. When the spreadsheet is used with a component that it is toggled from view using ngIf/ngFor it seems to not initialize correctly and causes a null exception.

We have managed to reproduce the problem with a very simple Angular application, here is the codesandbox example:

Here is the Angular template code of the example with the problem:

`<button (click)="onButtonClick(true)">Count Up</button>
    <spreadsheet *ngIf="counter % 2 === 0" [dummyCounter]="counter" />`

So the spreadsheet is only shown if counter mod 2 === 0.

You need to click in the spreadsheet edit line (the cell below the menu). After clicking the count button to make the spreadsheet appear/disappear. Eventually you will get an exception in your spreadsheet code in onselectionchange:

                                  onselectionchange: function () {
                                        var e = n.getRootView(),
                                            t = e && e.refs && e.refs.input_wrapper,
                                            i = "0px";
                                        if (n._isEdit && n._value && n._input) {
                                            var o = n._input.selectionStart,
                                                r = (0, d.getStringWidth)("".concat(n._value).substr(0, o)) + 12,
                                                a = n.getRootNode().offsetWidth;  // <----- exception here, n.getRootNode() returns null
                                            t && a < r && (i = a - r + "px");


Hopefully you can verify and find a solution.
Thank you.


Thank you for your report. The problem was confirmed. We’ll try to fix it in one of the future updates.
For now as a workaround in your “spreadsheet.component.ts” please, try to remove the selectionchange event on the component destroying:

  ngOnDestroy(): void {
//add the following code