How to share data between two modules in AngularJS

2019-01-11 10:14发布

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?

1条回答
趁早两清
2楼-- · 2019-01-11 11:06

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');
    }
});
查看更多
登录 后发表回答