I have an app mounted, which is structured as follows:
Menú
- Sección 1
- Sección 2
- Sección 3
Each of the sections is a view, which loads the data from an API, which is executed when accessing each of the views.
My question is, is there a way with AngularJS to keep the data even if we change views? That is, when accessing section 2 from 1, one does not lose the data when returning to it and does not have to consult the API again.
I am looking to be able to load the data only once, if not then I have to make calls every time I enter a view.
If there is a better way to do it, feel free to tell me.
EDIT 1
Right now I have it like this:
app.controller('IncidenciasCtrl', ['$scope', '$cookies', 'serviceDatosApi', function($scope, $cookies, serviceDatosApi) {
'use strict';
serviceDatosApi.devuelveIncidencias("gttp://192.168.1.1/llamadaAPI")
.then(function(datos){
$scope.incidencias = datos.data;
});
}]);
app.service('serviceDatosApi', ['$http', function ($http) {
this.devuelveIncidencias = function(url) {
return $http.get(url);
}
}]);
new answer
The promises maintain their state once they have been fulfilled, this means that if you call the promise again
then
on a promise that has already been fulfilled, it will invoke the callback with the result of the promise. That is, the promise already stores the result obtained by you! What remains to be done is store the promise.Leaving the service like this:
Now the problem with this is that once a Promise is resolved , you won't be able to get new URLs or voluntarily reload the same URL. To overcome this problem, I leave you an example: use a fix for the chache by URL and with the option to reload any URL.
Complete example: (you can check in the console that the XHR request is launched only once)
original answer
Assuming that the data is in a controller (and that is why it is lost when changing views, since the controller changes), what you can do is use a Service.
A
service
is a singleton that is born the first time it is injected and then, on subsequent injections, the same instance of the service will always be injected to all subsequent users (controllers) of the service itself.This makes it a good tool to share information throughout the life cycle of the web app and has the advantage that by pressing F5, when the web app is reloaded, the service is rebuilt and the data is lost.
There are many built-in services like:
$interval
,$http
,$location
and others. And you can make your own.Then in the controller what you have to do is inject the service (in all the controllers)
on another controller
Using this technique it is very easy to share resources/data between controllers and therefore between views.
There are many ways to do what you want, and those are:
I know the first two because I have worked with them, the last one I put on the list, I have never worked with, but it exists.
It all depends on how much information you are going to store, but seeing that you already have an application developed, I recommend using SQLite or WebSQL.
The difference between one and the other is that WebSQL does not depend on any plugin or add-on to use either in the browser or when the apk is already compiled, while SQLite only works on the device through an add-on, in your case you work with AngularJS (Angular 1), I don't know how advanced you are, I recommend using the Ionic Framework, I don't know if you know it but it would help you a lot.