2014-10-27 132 views
19

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

回答

30

希望以下实施将帮助您获得一些理解。

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

我有两个modules.frontApp和departmentApp.I需要从frontApp数据传递到departmentApp一些link.Do我有两个app.js使用上面的代码的点击? @Aviro – 2017-09-12 10:26:21

+0

如果这两个模块属于同一个应用程序,这是推荐的数据传输方式。 – Aviro 2017-09-14 03:19:51

+0

我有两个不同的ng-app.Is有可能通过调用函数从一个应用程序发送ID到另一个应用程序? – 2017-09-14 05:04:23

相关问题