ngx-drag-scroll icon indicating copy to clipboard operation
ngx-drag-scroll copied to clipboard

Error when there are no elements inside <drag-scroll> and scrollbar should be hidden

Open IlCallo opened this issue 5 years ago • 7 comments

  • Do you want to request a feature or report a bug? Bug

  • What is the current behavior? Since 7.1.4, package has an error if elements inside <drag-scroll> are inserted dynamically using *ngFor (or any other situation when there are 0 elements inside I guess) and scrollbar should be hidden.

  • If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem you can use the demo site as an example or via https://plnkr.co This is my setup.

<drag-scroll scrollbar-hidden="true" drag-scroll-y-disabled="true">
    <ng-container *ngFor="let item of items">
      // Stuff
    </ng-container>
  </drag-scroll>
  • What is the expected behavior? It should not fire off errors.

  • Please tell us about your environment:

    • Version: 7.3.1
    • Browser: [all]
  • Other information (e.g. detailed explanation, stacktraces, related issues, suggestions how to fix, links for us to have context, eg. stackoverflow, gitter, etc) The error.

ERROR TypeError: Cannot read property '_elementRef' of undefined
    at DragScrollComponent.push.../../node_modules/ngx-drag-scroll/fesm5/ngx-drag-scroll.js.DragScrollComponent.adjustMarginToLastChild (ngx-drag-scroll.js:1076)
    at DragScrollComponent.push.../../node_modules/ngx-drag-scroll/fesm5/ngx-drag-scroll.js.DragScrollComponent.ngAfterViewInit (ngx-drag-scroll.js:303)
    at callProviderLifecycles (core.js:22416)
    at callElementProvidersLifecycles (core.js:22390)
    at callLifecycleHooksChildrenFirst (core.js:22380)
    at checkAndUpdateView (core.js:23316)
    at callViewAction (core.js:23548)
    at execEmbeddedViewsAction (core.js:23511)
    at checkAndUpdateView (core.js:23308)
    at callViewAction (core.js:23548)
    at execComponentViewsAction (core.js:23490)
    at checkAndUpdateView (core.js:23313)
    at callViewAction (core.js:23548)
    at execEmbeddedViewsAction (core.js:23511)
    at checkAndUpdateView (core.js:23308)

It all comes down to these lines apparently

https://github.com/bfwg/ngx-drag-scroll/blob/357af5d6d2a9d2b8bb38e59949e460f3bc829e11/projects/ngx-drag-scroll/src/lib/ngx-drag-scroll.component.ts#L729-L736

where you are assuming that this._children['_results'] won't ever be an empty array (if I understand the code correctly). I think the else clause is actually never reached: if this._children['_results'].length is not >1, previous assignment will always fail throwing a TypeError.

IlCallo avatar May 08 '19 13:05 IlCallo

Hi @IlCallo , the drag-scroll-item directive is missing in your code example. Can you try the below?

<drag-scroll scrollbar-hidden="true" drag-scroll-y-disabled="true">
    <div drag-scroll-item>
       <ng-container *ngFor="let item of items">
             // Stuff
       </ng-container>
    </div>
  </drag-scroll>

bfwg avatar May 30 '19 00:05 bfwg

Well... It's missing because I'm not using the "carousel" feature 🤔 I want to use it with the "free dragging" experience, like it's written in the README

IlCallo avatar May 30 '19 09:05 IlCallo

hmm, I'm not sure what you mean by free dragging. Can you give me an example of what you are trying to achieve?

bfwg avatar May 31 '19 17:05 bfwg

This: https://github.com/bfwg/ngx-drag-scroll/blob/e4cca35d54ab38c779aa82ba7bef41542c10a709/README.md#L53-L71

I'm not interested in: https://github.com/bfwg/ngx-drag-scroll/blob/e4cca35d54ab38c779aa82ba7bef41542c10a709/README.md#L73

I should also note that, as soon as elements are actually added by the *ngFor, the package works as expected.

IlCallo avatar Jun 10 '19 09:06 IlCallo

Ok, this is blocking you in any way? If not, I'll try to find time to investigate later this week.

bfwg avatar Jun 11 '19 17:06 bfwg

No, absolutely, i came back to this because I have a free week and I'm going through all issues I opened last month to help close them, but no hurry

IlCallo avatar Jun 11 '19 19:06 IlCallo

I met exactly the same problem...

nemoyans avatar Jul 25 '19 08:07 nemoyans