We are no longer accepting contributions to Documentation. Please see our post on meta.

AngularJS

Sharing Data All Versions

0.9.0
0.9.1
0.9.2
0.9.3
0.9.4
0.9.5
0.9.6
0.9.7
0.9.9
0.9.10
0.9.11
0.9.12
0.9.13
0.9.14
0.9.15
0.9.16
0.9.17
0.9.18
0.9.19
0.10.0
0.10.1
0.10.2
0.10.3
0.10.4
0.10.5
0.10.6
1.0.0rc1
g3-v1.0.0rc1
g3-v1.0.0-rc2
v1.0.0rc2
v1.0.0rc3
v1.0.0rc4
v1.0.0rc5
v1.0.0rc6
v1.0.0rc7
v1.0.0rc8
v1.0.0rc9
v1.0.0rc10
v1.0.0rc11
v1.0.0rc12
1.0.0
1.0.1
1.0.2
1.1.0
1.0.3
1.1.1
1.0.4
1.1.2
1.0.5
1.1.3
1.0.6
1.1.4
1.0.7
1.1.5
1.2.0rc1
1.0.8
1.2.0-rc.2
1.2.0-rc.3
1.2.0
1.2.1
1.2.2
1.2.3
1.2.4
1.2.5
1.2.6
1.2.7
1.2.8
1.2.9
1.2.10
1.2.11
1.2.12
1.2.13
1.2.14
1.3.0-beta.1
1.3.0-beta.2
1.3.0-beta.3
1.2.15
1.3.0-beta.4
1.2.16
1.3.0-beta.5
1.3.0-beta.6
1.3.0-beta.7
1.3.0-beta.8
1.3.0-beta.9
1.3.0-beta.10
1.2.17
1.3.0-beta.11
1.2.18
1.3.0-beta.12
1.3.0-beta.13
1.2.19
1.3.0-beta.14
1.2.20
1.3.0-beta.15
1.3.0-beta.16
1.2.21
1.3.0-beta.17
1.2.22
1.3.0-beta.18
1.2.23
1.3.0-beta.19
1.3.0-rc.0
1.2.24
1.3.0-rc.1
1.2.25
1.3.0-rc.2
1.3.0-rc.3
1.3.0-rc.4
1.2.26
1.3.0-rc.5
1.3.0
1.3.1
1.3.2
1.3.3
1.2.27
1.3.4
1.3.5
1.3.6
1.3.7
1.2.28
1.3.8
1.4.0-beta.0
1.3.9
1.3.10
1.4.0-beta.1
1.3.11
1.4.0-beta.2
1.3.12
1.4.0-beta.3
1.3.13
1.4.0-beta.4
1.3.14
1.4.0-beta.5
1.3.15
1.4.0-beta.6
1.4.0-rc.0
1.4.0-rc.1
1.4.0-rc.2
1.4.0
1.3.16
1.4.1
1.3.17
1.4.2
1.4.3
1.4.4
1.3.18
1.4.5
1.3.19
1.4.6
1.5.0-beta.0
1.2.29
1.3.20
1.4.7
1.5.0-beta.1
1.5.0-beta.2
1.4.8
1.5.0-rc.0
1.5.0-rc.1
1.4.9
1.5.0-rc.2
1.5.0
1.4.10
1.5.1
1.5.2
1.5.3
1.5.4
1.5.5
1.4.11
1.5.6
1.4.12
1.5.7
1.2.30
1.5.8
1.2.31
1.4.13
1.2.32
1.6.0-rc.0
1.6.0-rc.1
1.5.9
1.6.0-rc.2
1.6.0
1.5.10
1.6.1
1.5.11
1.6.2
1.6.3
1.6.4
1.6.5

This draft deletes the entire topic.

Examples

  • 9

    Firstly, include the ngStorage source in your index.html.

    An example injecting ngStorage src would be:

    <head>
        <title>Angular JS ngStorage</title>
        <script src = "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
        <script src="https://rawgithub.com/gsklee/ngStorage/master/ngStorage.js"></script>
    </head>
    

    ngStorage gives you 2 storage namely: $localStorage and $sessionStorage. You need to require ngStorage and Inject the services.

    Suppose if ng-app="myApp", then you would be injecting ngStorage as following:

    var app = angular.module('myApp', ['ngStorage']);
            app.controller('controllerOne', function($localStorage,$sessionStorage) {
            // an object to share
            var sampleObject = {
                name: 'angularjs',
                value: 1
            };
           $localStorage.valueToShare = sampleObject;
           $sessionStorage.valueToShare = sampleObject;
        })  
    .controller('controllerTwo', function($localStorage,$sessionStorage) {
        console.log('localStorage: '+ $localStorage +'sessionStorage: '+$sessionStorage);
    })
    

    $localStorage and $sessionStorage is globally accessible through any controllers as long as you inject those services in the controllers.

    You can also use the localStorage and sessionStorage of HTML5. However, using HTML5 localStorage would require you to serialize and deserialize your objects before using or saving them.

    For example:

    var myObj = {
        firstname: "Nic",
        lastname: "Raboy",
        website: "https://www.google.com"
    } 
    //if you wanted to save into localStorage, serialize it  
    window.localStorage.set("saved", JSON.stringify(myObj));  
    
    //unserialize to get object  
    var myObj = JSON.parse(window.localStorage.get("saved"));
    
  • 2

    We can create a service to set and get the data between the controllers and then inject that service in the controller function where we want to use it.

    Service :

    app.service('setGetData', function() {
      var data = '';
        getData: function() { return data; },
        setData: function(requestData) { data = requestData; }
    });
    

    Controllers :

    app.controller('myCtrl1', ['setGetData',function(setGetData) {
    
      // To set the data from the one controller
      var data = 'Hello World !!';  
      setGetData.setData(data);
    
    }]);
    
    app.controller('myCtrl2', ['setGetData',function(setGetData) {
    
      // To get the data from the another controller  
      var res = setGetData.getData();
      console.log(res); // Hello World !!
    
    }]);
    

    Here, we can see that myCtrl1 is used for setting the data and myCtrl2 is used for getting the data. So, we can share the data from one controller to another contrller like this.

Please consider making a request to improve this example.

Syntax

Syntax

Parameters

Parameters

Remarks

A very common question when working with Angular is how to share data between controllers. Using a service is the most frequent response and this is a simple example demonstrating a factory pattern to share any type of data object between two or more controllers. Because it is a shared object reference, an update in one controller will be immediately available in all other controllers using the service. Note that both service and factory and both providers.

Still have a question about Sharing Data? Ask Question

Topic Outline


    We are no longer accepting contributions to Documentation. Drafts cannot be modified.