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?

isherwood
  • 58,414
  • 16
  • 114
  • 157
KDKR
  • 365
  • 2
  • 3
  • 11

1 Answers1

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');
    }
});
Aviro
  • 2,125
  • 22
  • 28
  • 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? @Aviro – Sachin HR Sep 12 '17 at 10:26
  • If the two modules belongs to the same app, this is the recommended way to transfer data. – Aviro Sep 14 '17 at 03:19
  • I am having two different ng-app.Is it possible to send id from one app to another app by calling function? – Sachin HR Sep 14 '17 at 05:04
  • Here is the link to my question.If you find some time plz go through.https://stackoverflow.com/questions/46193464/passing-id-from-one-controller-to-another-of-different-app-js-files – Sachin HR Sep 14 '17 at 05:06