Home Web Front-end JS Tutorial Detailed explanation of local storage instances of AngularJs data

Detailed explanation of local storage instances of AngularJs data

Jun 27, 2017 am 11:52 AM
angularjs javascript storage

This article mainly introduces how each independent JS file or different controller realizes data sharing and interaction. It has a certain reference value, let’s take a look with the editor below

First, create a factory to store and retrieve your data (you can create a separate js file and name it according to semantics Such as: dataService.js. Then introduce this JS file on your main page)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

<!--引入到你的主页面里面-->

<script src="dataService.js"></script>

创建一个factory

&#39;use strict&#39;;

angular.module(&#39;myApp&#39;)

.factory(&#39;datadService&#39;,[&#39;$window&#39;,function($window) {

 return{

 //存储单个属性

  set :function(key,value){

  $window.localStorage[key]=value;

  },

  //读取单个属性

  get:function(key,defaultValue){

  return $window.localStorage[key] || defaultValue;

  },

  //存储对象,以JSON格式存储

  setObject:function(key,value){

  $window.localStorage[key]=JSON.stringify(value);

  },

  //读取对象

  getObject: function (key) {

  return JSON.parse($window.localStorage[key] || &#39;{}&#39;);

  }

 }

}]);

Copy after login

Second, inject the method module [datadService] you created into the controller you want. The following controller is [productCtrl]. Next we create a set.js file with the following code:

1

2

3

4

5

6

7

8

9

10

11

12

13

&#39;use strict&#39;;

angular.module(&#39;myApp&#39;).controller(

 &#39;productCtrl&#39;,

 [ &#39;$scope&#39;,&#39;datadService&#39;,

 function($scope, datadService) {

 $scope.appiAppType = 1;

 //这里面$scope.appiAppType的赋值同样可以通过$http.post或者$http.get

 //等方法返回的参数去赋值,例子如下:

 //$http.post(&#39;这里是你所要访问的接口【URL】&#39;,这里是你想要上传的参数).success(function(data){

   // $scope.appiAppType = data;

   //});

 datadService.setObject("lodinData", $scope.appiAppType);// 将你获取来的数据存储到你之前创建的【datadService】中,这里面的【lodinData】是KEY(个人理解就是你把数据存到大箱子里面这个箱子就是【datadService】,为了方便在这个箱子里面更好的寻找你想要的数据就给他一个小标签,那就是【lodinData】)

 } ]);

Copy after login

Third, how to obtain stored data in different controls Okay, let’s create a get.js with the following code:

1

2

3

4

5

6

7

8

9

10

11

12

13

&#39;use strict&#39;;

//首先大家要把之前创建好的模块也就是那个装数据的箱子【datadService】放到这个控制器中(也就是模块注入)

//其次大家通过之前咱们设定的标签【lodinData】,用【getObject(&#39;key&#39;)】方法取到你想要的数据;

//具体实现就一行代码:datadService.getObject(&#39;lodinData&#39;);「注:把箱子拿出来(datadService)用(getObject)去拿你的这个(lodinData)标签下的数据」

angular.module(&#39;myApp&#39;).controller(

 &#39;completeCtrl&#39;,

 [ &#39;$scope&#39;, &#39;datadService&#39;,

 function($scope, datadService) {

 //我们这里取到来上面已经存好的数据:【datadService.getObject(&#39;lodinData&#39;);】并且把这个数据赋值给了【$scope.LoginList】

 $scope.LoginList = datadService.getObject(&#39;lodinData&#39;);

 //这里大家可以打印一下$scope.LoginList 看看里面是什么;

 alert(JSON.stringify($scope.LoginList))

 } ]);

Copy after login

The above is the detailed content of Detailed explanation of local storage instances of AngularJs data. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

Huawei will launch innovative MED storage products next year: rack capacity exceeds 10 PB and power consumption is less than 2 kW Huawei will launch innovative MED storage products next year: rack capacity exceeds 10 PB and power consumption is less than 2 kW Mar 07, 2024 pm 10:43 PM

This website reported on March 7 that Dr. Zhou Yuefeng, President of Huawei's Data Storage Product Line, recently attended the MWC2024 conference and specifically demonstrated the new generation OceanStorArctic magnetoelectric storage solution designed for warm data (WarmData) and cold data (ColdData). Zhou Yuefeng, President of Huawei's data storage product line, released a series of innovative solutions. Image source: Huawei's official press release attached to this site is as follows: The cost of this solution is 20% lower than that of magnetic tape, and its power consumption is 90% lower than that of hard disks. According to foreign technology media blocksandfiles, a Huawei spokesperson also revealed information about the magnetoelectric storage solution: Huawei's magnetoelectronic disk (MED) is a major innovation in magnetic storage media. First generation ME

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Git installation process on Ubuntu Git installation process on Ubuntu Mar 20, 2024 pm 04:51 PM

Git is a fast, reliable, and adaptable distributed version control system. It is designed to support distributed, non-linear workflows, making it ideal for software development teams of all sizes. Each Git working directory is an independent repository with a complete history of all changes and the ability to track versions even without network access or a central server. GitHub is a Git repository hosted on the cloud that provides all the features of distributed revision control. GitHub is a Git repository hosted on the cloud. Unlike Git which is a CLI tool, GitHub has a web-based graphical user interface. It is used for version control, which involves collaborating with other developers and tracking changes to scripts and

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

See all articles