Detailed explanation of the use of $cachefactory cache in angular _angularjs

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 be referenced in the controller in the project, there are several existing forms of Service, factory (), services (), constant (), value ();p rovider (), where 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; Service is an important form of call between each controller, in the actual project will use a lot, the following is code:

Angular.module (' YourApp '). Factory (' Mycache ', function ($cacheFactory) {return
$cacheFactory (' MyData ');

Here ' Mycache ' is a service name and is unique to a specific cache object that exists in the browser for controller reference:

Angular.module (' YourApp ').
Controller (' Userctrl ', [' $scope ', ' $http ', ' Mycache ', function ($scope, $http, Mycache) {//monitor if there is already a cached data, and if so, what do we do?
var cache=mycache.get (' MyData ');
if (cache) {
$scope. Variable=cache
} else{
//From the interface to get data, put to 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

The first time the page is opened, the data is fetched from the interface. When the page occurs the route jumps, these data is always cached, the route jumps to the demand page, the monitoring has already been cached, does not have to request the data to the server, if is the page is closed or is refreshes the page, the cache will lose, afresh, requests the data and caches ;

This is a simple example, the data in the actual project will be more complex, such as the page optimization for the reduction of the data collection request, reduce the number of data requests, get the data allocated after the route of the various template, the data structure will be more complex;

There is no large cache of data samples, in theory here is the cache is a JavaScript object, so the size of the application cache or the size of the browser page performance impact, is not very clear, continue to deep grilled, found a different point to further.

The above is a small set to introduce the details of the angular $cachefactory cache use, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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: 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.