How to share data between two modules in AngularJS?

KDKR picture KDKR · Oct 27, 2014 · Viewed 15.7k times · Source

I am using AngularJS along with c# mvc. I have a home page where user enters some data and that should be passed to the second module where I use this data for processing and decisions. I have to use the data entered or updated in the first module within the second module. Can someone help me how to achieve this?

Answer

Aviro picture Aviro · Oct 27, 2014

Hope the following implementation will help you to get some understanding.

angular.module('app.A', [])
.service('ServiceA', function() {
    this.getValue = function() {
        return this.myValue;
    };

    this.setValue = function(newValue) {
        this.myValue = newValue;
    }
});

angular.module('app.B', ['app.A'])
.service('ServiceB', function(ServiceA) {
    this.getValue = function() {
        return ServiceA.getValue();
    };

    this.setValue = function() {
        ServiceA.setValue('New value');
    }
});