How to share data between two modules in AngularJS?
Asked Answered
J

1

21

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?

Jackpot answered 27/10, 2014 at 18:16 Comment(0)
C
32

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');
    }
});
Chimene answered 27/10, 2014 at 22:16 Comment(4)
I have two modules.frontApp and departmentApp.I need to pass data from frontApp to departmentApp on click of some link.Do i have to use above code in both app.js? @ChimeneSiegel
If the two modules belongs to the same app, this is the recommended way to transfer data.Chimene
I am having two different ng-app.Is it possible to send id from one app to another app by calling function?Siegel
Here is the link to my question.If you find some time plz go through.#46193964Siegel

© 2022 - 2024 — McMap. All rights reserved.