Recently, I have been learning to use angular and slowly transforming from jquery ui to using ng development. I have found many differences, so continue to learn;
First, create a service to reference it in the controller in the project. There are several forms of service existence, factory();service();constant();value();provider(); among them, provider is the most basic, and other services are written based on this. I won't expand the specific differences here. You can take a look at the source code; services are an important form of calls between controllers, and they will be used a lot in actual projects. The following is the code:
angular.module('yourApp').factory('myCache',function($cacheFactory){return $cacheFactory('myData');});Here 'myCache' is the service name, and is unique, for a specific cache object, exists in the browser, for reference by the controller:
angular.module('yourApp').controller('userCtrl',['$scope','$http','myCache',function($scope,$http,myCache){ //Monitor whether cached data already exists, if there is, get it and what should I do, go to var cache=myCache.get('myData'); if(cache){$scope.variable=cache;}else{//Get data from the interface and put into the cache var jurl='/data/getdata';$http({url: jurl,method: "GET",data: "{'query':'somevalue'}",headers: { 'Content-Type': 'application/json' }}).success(function (data, status, headers, config) {//something in success }).error(function (data, status, headers, config) {//something in error});}}])When the page is opened for the first time, data will be obtained from the interface. When the page is routed, these data will be cached all the time. The route will jump to the page with demand. If it is monitored that it has been cached, there is no need to request data from the server. If the page is closed or the page is refreshed, the cache will be lost, and the data will be requested again and cached;
This is a simple example. The data in actual projects will be more complicated. For example, in order to reduce page optimization, the page data collection is requested, the number of data requests is reduced, and the data structure will be more complicated in each template of the route assigned after data is obtained;
There is currently no example of cached large data. Theoretically, the cache here is an object object in JavaScript, so the impact of the size limit or size of the cache in the application on the performance of the browser page is not very clear. Let's continue to dig deeper and find the differences before continuing.
The above is a detailed explanation of the use of $cacheFactory cache in Angular introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!