Angularjs如何将数据从一个模块发送到另一个模块

时间:2015-05-12 11:59:16

标签: javascript angularjs

我有一个像这样的模块

(function() {
  var app = angular.module('build', ['ngSanitize', 'uihelper']);

  app.controller('buildctrl', ['$scope', function($scope) {
     $scope.attributes = {"color": "#efefef", "width": "20px" .... };
...

我的uihelper模块看起来像这样..

(function() {
  var app = angular.module('uihelper', []);

  app.controller('uictrl', ['$scope', function($scope) {
  $scope.data = // I want $scope.attributes here

...

我是否可以访问模块2中模块1中的$scope.attributes,模块2是否会观察模块1中$scope.attributes的动态更改

我刚开始使用angularjs。我感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

使用方法创建服务,在任何需要的地方注入服务

app.service('ConstantsService', function () {
    return {
        getAttributes: {"color": "#efefef", "width": "20px"}
    }
})


app.controller('uictrl', function ($scope, ConstantsService) {
    $scope.data = ConstantsService.getAttributes();
})