Collection Repeat giving error on using function on item-height

When the array which feeds the collection repeat is empty, it throws an error on console -

The thing is that my array is empty in the beginning. Then it is filled by data with an AJAX request.
But, before the data comes, this error comes on the screen.

Is this a bug or am I handling the situation incorrectly ?

it seems like collection repeat triggers at least 1 even if your list is empty.
add a ng-if on the collection-repeat node and check if items is defined and has a length

1 Like

If I add this to the collection-item div - ng-if ="items.length>0".
Then it still goes in the getItemHeight function and if I alert the length of the array in the getItemHeight it gives 0

So like this?

1 Like

but the strange thing is that collection repeat calls the method while there is no item in the items list


Yes that works…but when there is no item then also the collection-repeat tries to calculate the height.
Not only does this give an error on console, I have a screen where I am using a scrollBottom() and the screen remains stuck in the middle. Unfortunately I am unable to share that exact code, but I have a feeling there is a bug in collection-repeat

1 Like

@Dbms which version of ionic are you using? I use collection repeat and dont initialize my scope items until after my ajax response comes back and I don’t get any errors.

Also, is there a reason you need to recalculate the height or use the item-height item-width properties? As of rc0, you can drop in collection-repeat as if you were using ng-repeat and don’t even need to use item-height or item-width. You could just have a css class for your item that specifies height/width. If you are using a verison before rc0 then I would strongly recommend upgrading as collection-repeat as significantly improved :smile:

I am using Ionic Version 1 RC 1.
I do need to use the item-height as all list items in my case are not necessarily of the same height.
Regarding initialising the scope items after the AJAX, the issue is that I have just moved from Beta 6 to RC1. I know it’s a huge jump, but most of the things are still working as is, except collection-repeat. This I understand has undergone major improvements but still what used to work earlier should work in later versions.

In any case, I will try to do what you have suggested and check if my other business logic is getting effected. Hopefully it should not, but just in case.

nope if you do not set height or width ionic calculates the missing dimensions of the first item and reuse this for the other

1 Like

@djett I did try what you said but that didn’t work. In fact if you don’t declare the array on which you are iterating, it still goes in the function specified in the item-height.
In case you make it work by forking my codepen, it would be helpful.

@Dbms your collection-repeat element should nested inside your ng-if.

see the following codepen… No errors

Yup. Already implemented that for the time being. It should have worked out of the box though. Thanks for the efforts :)

1 Like