21

我正在使用 AngularJS 和 c# mvc。我有一个主页,用户可以在其中输入一些数据,并且应该将其传递到第二个模块,在该模块中我使用这些数据进行处理和决策。我必须使用在第二个模块中的第一个模块中输入或更新的数据。有人可以帮助我如何实现这一目标吗?

4

1 回答 1

32

希望下面的实现能帮助你得到一些理解。

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');
    }
});
于 2014-10-27T22:16:09.610 回答