如何在 AngularJS 中的两个模块之间共享数据? [英] How to share data between two modules in AngularJS?

查看:29
本文介绍了如何在 AngularJS 中的两个模块之间共享数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

希望下面的实现能帮助你有所了解.

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');
    }
});

这篇关于如何在 AngularJS 中的两个模块之间共享数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆