I have a template with a list in it. My Controller gets data from a service which is added to $scope in success function. So ng-repeat renders the list. After that I want to scroll to a specific item in the list but thats not possible because the rendering of the view is not finished when the $anchorScroll is called.
In this http://jsfiddle.net/dnzY9/ they use $timeout and 1000ms. Thats 1second so it slows down the whole app for the user. I think thats not a good thing because you fetch data from a serviceâŚthat needs time and then you add 1sec to this time.
Agreed, adding an additional second is pretty bad for UX, but the example was meant to show that if you use a $timeout, angular will process that code in the next scope digest. So the additional time of 1000ms is irrelevant
@roland I have a really rough solution for you. It needs lots of cleanup(remove watchers if the data will not update, etc).
Iâve created a directive âdelayed-scrollâ. The directive needs the attributes âscroll-toâ and âtotal-itemsâ. These are pretty self explanatory.
The controller must broadcast an event when the async call is completed. This could be removed, but I didnât want the directive âwatchingâ all day long.
Once the âitems-loadedâ event is received, the directive will $watch the child elements of the list. When the list contains the exact same number of elements as âtotal-elementsâ, you know the list is rendered. Then, the directive sets the anchor tag in the location and calls $ionicScrollDelegate.anchorScroll().
Right now, Iâm using a list of 500 items that are returned after a timeout of 1200ms. It then picks some random item to scroll to. It works perfectly but probably would be considered a pretty ugly solution.
UPDATE : Iâve tried it with as many as 5000 records and it still works - with a big deal due to generating such a big list.
Your support is just awesome. Very fast and always helping.
@mhartington I donât really understand why the 1000ms are irrelevant? Maybe I donât understand $timeout good enough. What did you mean with the next scope digest?
@Calendee
Thank you for your effort. I looked at it and I understand it. Maybe it would be cool for further versions of ionic to implement such a solution. I think many people will need to do something after a list or content is rendered completely.
Why did you say its a rough or ugly solution. What should we do to make a good solution of it? I am not an directive or angular expert so my question is maybe silly. =)
OK so does this 1200ms timeout really happen? Because if i try your codepen I canât recognize it. And why exactly 1200ms?
I said âuglyâ because someone on the Ionic dev team could make this more stable and elegant than I have here. It works and I wouldnât hesitate to use it if I needed to. However, the Ionic team could do it much better. Iâm a bit of a hack.
1200ms : I just made that number up. You can change it to anything you like. I just wanted to simulate the delay of loading the content over a slow network connection. Play around with the number to see the different effect.
If you feel this type of feature is important, then just open a feature request on the Ionic GitHub issues page. Iâm sure it will be considered.
@roland : The 1200ms means nothing at all. Since the controller is providing fake data, it will populate the list immediately. So, I simply put it in there to simulate how waiting for a slow connection to return data would feel.
With a REAL AJAX call, the controller would populate the list in whatever time it took to get a response from the server. This solution would work in fast responses and slow responses.