I have a gallery with multiple images.
When clicking on an image it goes to an individual view.
When clicking back
button it goes to the gallery but the user experience isn't optimal - all the images are loading again!
To observe the loading go to Chrome Dev Tools
and enable Network Throttling
(placeholder images are not the best example as they load almost instantaneous)
I know that in order to preserve state of the controller I can use a service and that's exactly what I'm doing: http://jsbin.com/doneqoruta/edit?html,output
app.service("files", function() {
return [
"350x150",
"450x150",
"550x150",
"650x150",
"750x150",
"850x150",
"350x150",
"450x150",
"550x150",
"650x150",
"750x150",
"850x150",
];
})
app.controller("ListCtrl", function($scope, files) {
console.log("LIST CTRL");
$scope.files = files;
});
Related question: Maintain model of scope when changing between views in AngularJS
Putting the files
into $rootScope
works for me, as suggested by this answer: https://stackoverflow.com/a/12961220/775359 by Mark Rajcok
$rootScope is a big global variable, which is fine for one-off things, or small apps.
Not-flickering version using $rootScope
:
app.controller("ListCtrl", function($rootScope, files) {
console.log("LIST CTRL");
if (!$rootScope.files) {
$rootScope.files = files;
}
});
Is there a better, more angular way?
In Ionic there is $ionicView.enter
and by asking this question I'm hoping to expand by knowledge.
UPDATE:
5 seconds video from the localhost: https://youtu.be/HU94T6RtEf8
(when clicking back I'd much rather have a smooth transition, I think the $rootScope
might be the way)