Use of $cachefactory cache in angular

Source: Internet
Author: User

Recently in learning to use angular, slowly from the jquery UI transformation to use NG development, found a lot of different points, continue to learn it;

First create a service to reference in the controller in the project, the service has several forms of existence, factory (), service (); constant (); value ();p Rovider (); provider is the most basic, Other services are based on this write, the specific difference here will not unfold, we can look at the source code; Service is an important form of communication between controllers, in the actual project will use a lot, 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 that exists in the browser for the controller to reference:

Angular.module (' YourApp '). Controller (' Userctrl ', [' $scope ', ' $http ', ' Mycache ',function($scope, $http, Mycache) {
Monitor if the cache data already exists, and if so, what to do.varCache=mycache.get (' MyData '); if(cache) {$scope. Variable=Cache; }Else{ //Fetch data from interface, put into cache varJurl= '/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 }); }}])

The first time the page is opened, the data will be fetched from the interface, when the page has a routing jump, the data is always cached, the route jumps to the required page, the monitoring has been cached, there is no need to request data to the server, if the page is closed or refresh the page, the cache will be lost, re-request data and cache ;

This is a simple example, in the actual project data will be more complex, such as the page optimization to reduce the page data collection request, reduce the number of data requests, to obtain the data after the allocation of the route of the template, data structure will be more complex;

There is no example of caching the larger data, in theory, the cache is a JavaScript object, so the application of the cache size limit or size of the browser page performance impact, is not very clear, continue to deep-grilled, found that the different points and more.

Use of $cachefactory cache in angular

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.