简体   繁体   English

不同HTML文件中模块之间的AngularJS通信

[英]AngularJS communication between modules in different html files

There are a couple of variables I want to share between two html files and two modules. 我想在两个HTML文件和两个模块之间共享几个变量。 Firstly, I used the service as follows but it did not work. 首先,我按以下方式使用了该服务,但它没有起作用。 In vote.js: 在vote.js中:

var vote = angular.module('vote', []);
vote.service('voteParam', function () {
this.candidates = [];
this.show_result;
this.vote_begin;
this.elec_begin;
this.setValue = function (voteParam) {
    this.department = voteParam.department;
    this.group = voteParam.group;
    this.type = voteParam.type;
    this.round = voteParam.round;
    this.times = voteParam.times;
    this.advance_num = voteParam.advance_num;
 }
});

In vote-setting.js: 在voting-setting.js中:

angular.module('admin')
.controller('vote_setting', function ($scope, $http, voteParam) {
 $scope.begin_vote = function () {
        init();
        startit();
        $scope.vote_begin = true;
        $scope.elec_begin = true;
        voteParam.vote_begin = true;
        voteParam.elec_begin = true;
        voteParam.show_result = false;
        voteParam.setValue($scope.voteParam);
        if (voteParam.candidates.length == 0) {
            $scope.get_candidate();
        }
    };
}

When the value is set as true in vote-setting.js, it is showed as undefined in vote.js. 当在表决设置.js中将该值设置为true时,在表决.js中显示为未定义。 I think it's because when I import vote-setting.js in vote-setting.html, variables in the service are initialized again. 我认为这是因为,当我在vote-setting.html中导入vote-setting.js时,服务中的变量会再次初始化。 Is there any other way I can make this work? 我还有其他方法可以使这项工作吗?

The service worked. 该服务有效。 But the value of variables in it get initialized when the js file is imported again. 但是,当再次导入js文件时,其中的变量值会初始化。 This is the key of this problem. 这是这个问题的关键。 Thanks 谢谢

Implement your shared data service as a factory. 将共享数据服务实施为工厂。

Implement proper getter and setters. 实现适当的getter和setter。

Encapsulate your variables and only expose the getter, setters. 封装变量,仅公开getter,setter。

Example of shared data service (can also be used as a shared Model) 共享数据服务的示例(也可以用作共享模型)

app.factory('voteParam', function() {
  var param1;
  var param2;
  var param3;

  return {
    setParam1: function(value){
      param1 = value;
    },
    getParam1: function(){
      return param1;
    },
    setParam2: function(value){
      param2 = value;
    },
    getParam2: function(){
      return param2;
    },
    setParam3: function(value){
      param3 = value;
    },
    getParam3: function(){
      return param3;
    }
  }
});

In an application this factory should be initialized once only. 在应用程序中,该工厂仅应初始化一次。

If you are using 2 different angular applications (in your case 'vote' and 'admin') then use local storage or session storage to share data between them 如果您使用2个不同的角度应用程序(在您的情况下为“ vote”和“ admin”),则使用本地存储或会话存储在它们之间共享数据

You might want to know the following: 您可能需要了解以下内容:

Generally,an angular app should have only one base module. 通常,角度应用程序应仅具有一个基本模块。 This is the module which you add to your html using ng-app directive. 这是您使用ng-app指令添加到html中的模块。 So your html content will know about only one angular app. 因此,您的html内容将只知道一个角度应用程序。

You can however create as many other modules as you would like or need. 但是,您可以根据需要创建任意数量的其他模块。 These can be custom modules as in your case, or these can be third party library (modules written by other developers for your ready use.). 这些可以是您自己的自定义模块,也可以是第三方库(其他开发人员编写的模块,可供您随时使用)。 In any of the above case you need to add extra modules to your base module as dependencies as shown below: 在上述任何一种情况下,您都需要将其他模块作为依赖项添加到基本模块中,如下所示:

angular.module('baseModule', ['extraModule1, extraModule2]); angular.module('baseModule',['extraModule1,extraModule2]);

Once you are done with this, any component like any directive , service , factory written in any of the extra modules shall be available to you for use, as if they were all written in your baseModule . 完成此操作后,在任何其他模块中编写的任何组件(如directiveservicefactory )都将可供您使用,就好像它们都是在您的baseModule编写的baseModule

Having said this and coming to your question, the code provided by you doesn't seem sufficient to entirely diagnose your issue. 这么说并提出您的问题,您提供的代码似乎不足以完全诊断您的问题。 However from whatever I get, I suggest you to add vote module to admin module or vice-versa. 但是无论从我得到的是什么,我建议您将vote模块添加到admin模块,反之亦然。 If you have any other module as a base module, you need to add both vote and admin as dependencies to that module. 如果您有其他任何模块作为基本模块,则需要同时添加voteadmin作为该模块的依赖项。

Hope this helps you to start fixing the issue. 希望这可以帮助您开始解决此问题。 Let me know if any thing seems unclear yet. 让我知道是否还有任何不清楚的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM