Skip to content
This repository has been archived by the owner on Jan 30, 2024. It is now read-only.

Cannot read property 'insertAdjacentElement' of undefined #24

Open
allahyar opened this issue Mar 11, 2020 · 8 comments
Open

Cannot read property 'insertAdjacentElement' of undefined #24

allahyar opened this issue Mar 11, 2020 · 8 comments

Comments

@allahyar
Copy link

Angular : 9.0.1
ngx-sticky-directive : 1.2.8

image

@dianjuar
Copy link
Member

Hello @allahyar can you provide more context about your problem?

A stackblitz link would bring more clarity

@yossely
Copy link
Member

yossely commented Apr 7, 2020

Hi

TL; DR

Pass the triggerOn id value this way [triggerOn]="'text-content'":

  <div ngxSticky [triggerOn]="'text-content'">
    Sticky Title
  </div>
  <div id="text-content">
    Text
  </div>

We found the problem of that bug, it's because Ivy, we have 3 options to solve the error:

  1. Recommended Pass the triggerOn id value this way:
  <div ngxSticky [triggerOn]="'text-content'">
    Sticky Title
  </div>
  <div id="text-content">
    Text
  </div>

Ivy changed something about how we pass parameters. There is an issue open about it

According to Ivy Compatibility guide

Unbound inputs for directives (e.g. name in ) are now set upon creation of the view, before change detection runs (previously, all inputs were set during change detection).

  1. Another option is to pass an HTMLElement as the triggerOn property like this:
  <div ngxSticky [triggerOn]="textContent">
    Sticky Title
  </div>
  <div #textContent>
    Text
  </div>
  1. The No recommended solution is to temporarily turn off Ivy, this can be done by adding the next option to your app's tsconfig.json file:
{
  "compilerOptions": {...},
  "angularCompilerOptions": {
    "enableIvy": false
  }
}

@yossely
Copy link
Member

yossely commented Apr 7, 2020

We will close this issue after adding this note to the README as part of the documentation and deploy library (so the doc is updated there too).

@METACEO
Copy link

METACEO commented May 9, 2020

Hi @yossely, I found this issue via angular/angular#34227 and I am trying to piece together an ivy directive myself: https://github.com/METACEO/ng-transition

While I believe we're facing the same issue, I'm experimenting with your first recommendation and I'm not having any luck (against my own directive) and our directive's internals look the same: @Input() and set

Does your first recommendation work for you?.. or are you proposing it as a change?

@yossely
Copy link
Member

yossely commented May 9, 2020

Hi @METACEO, yes, we're facing the same issue and yes, my first recommendation works in my case, passing the string property as [triggerOn]="'text-content'" my directive works as expected.

In my case, I wanted to get an element by id which is indicated by the triggerOn value and the view was not rendered yet, by changing the binging to [triggerOn]="'text-content'" the view was ready and then the element could be found. Do you have the same case?

@METACEO
Copy link

METACEO commented May 9, 2020

by changing the binging to [triggerOn]="'text-content'" the view was ready and then the element could be found. Do you have the same case?

I unfortunately begin to receive a Can't bind to 'ngTransitionEnter' since it isn't a known property of 'div' error when I wrap up my attributes with the [] brackets.

A demo of the error: https://stackblitz.com/edit/angular-ivy-rlsxgb?file=src%2Fapp%2Fapp.component.html

And I look my directive's source and it looks identical to ngx-sticky (in terms of @Input() and set) and if I remove the [] brackets, it will compile but the set never occurs (determined with breakpoints.)

I don't know if this is the same for ngx-sticky but if I hard-lock my consuming project to angular 9.0.0, everything works. The moment I get the latest (angular ^9.1.0) then this issue occurs.

Thank you @yossely for verifying that your first recommendation works, I'll work towards that!

// ngx-sticky
@Input()
set triggerOn(value: string | ElementRef | HTMLElement) {
  this.setHTMLElement('_triggerOn', value);
}

// ng-transition
@Input()
set ngTransitionEnter(ngTransitionEnter: string) {
  this._ngTransitionEnter = ngTransitionEnter.split(' ');
}

@METACEO
Copy link

METACEO commented May 9, 2020

@yossely I believe I've determined that my issue was the conditional template I was rendering with the *ngTransition directive. I've changed the directive to work with the [ngTransition] binding and things are looking much better for the directive and its other attributes, too.

After reading the "Ivy compatibility examples" page, I think the conditional template was interfering with the attributes being set. It wasn't the change I was hoping to find, but it should make due in the meantime. Your first recommendation works just well for me!

Thank you again for the support and replying earlier!

@yossely
Copy link
Member

yossely commented May 12, 2020

@METACEO you're welcome! Glad to hear that you could solve it too 👍

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants