0

I am working on chat app, and the problem seems to be that directive for scroll bottom, to the last item in ng-repeat is not working properly when there are images in messages. So it just doesn't scroll all the way bottom. I think the reason is that directive scrolls before the image is fully loaded.

                  // Scroll to bottom directive
                    .directive('schrollBottom', function ($timeout) {
                      return {
                        scope: {
                          schrollBottom: "="
                        },
                        link: function (scope, element) {
                          scope.$watchCollection('schrollBottom', function (newValue) {
                            if (newValue)
                            {
                             $timeout(function() { 

                          $(element[0]).scrollTop($(element)[0].scrollHeight);
                         }, 0, false);



                            }
                          });
                        }
                      }
                    })
6
  • Check here to see how to wait for an image to load before doing something. You can add a class to all images and wait for all images with that class to load before scrolling. Commented Aug 2, 2017 at 11:49
  • @Lansana, Thanks is there is a way to have things in one directive, one is watch the collection and second condition if image loaded? Commented Aug 2, 2017 at 12:12
  • I would keep what logic you have, and put it inside of an img.onload callback handler. Then any scrolling to the bottom will only occur once all images have loaded. Commented Aug 2, 2017 at 12:19
  • @Lansana, thank you. you can mark as answer i will accept it. Commented Aug 2, 2017 at 13:03
  • Answered :) glad I could help! Commented Aug 2, 2017 at 13:37

1 Answer 1

1

Check here to see how to wait for an image to load before doing something. You can add a class to all images and wait for all images with that class to load before scrolling.

I would keep what logic you have, and put it inside of an img.onload callback handler. Then any scrolling to the bottom will only occur once all images have loaded.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.