Marc Rasmussen Marc Rasmussen - 4 months ago 11
AngularJS Question

Angularjs resources keeps loading when changing page

Okay so i have a pretty "Hardcore" application content and resource wise where people are able to upload multiple videos, images documents ect.

Now looking at my network tab in chrome i saw something disturbing:

Page 1 has a video the user goes to page 1 and clicks play on the video

The user then decides that he wants to change page and therefor he goes to page 2.

While the user is on page 2 the video from the previous page is still getting chunks meaning that it is still loading. So if you continue this pattern for long enough the videos will start stalling.

I am guessing that i am not the first to come across this issue and hope that some of you have a way to solve it.

This is how i load my videos:

<video id="player1" src="http://mydomain/folder/video.mp4" controls="controls" width="598" height="320">
</video>

Answer

You could write a directive that does this on your video elements;

angular.module('myApp').directive('stopLoadOnChange', ['$rootScope', function($rootScope) {
    return {
        restrict: 'A',
        link: function(scope, element, attrs) {

            $rootScope.$on('$viewContentLoaded', function() {
                element.get(0).suspend();

            });
        }
    }
}]);

and use it like this:

<video stop-load-on-change id="player1" src="http://mydomain/folder/video.mp4" controls="controls" width="598" height="320">
</video>