Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 通过共享服务在两个不同ng应用程序中的控制器之间共享数据不工作_Javascript_Angularjs_Angularjs Scope_Angularjs Service_Angularjs Factory - Fatal编程技术网

Javascript 通过共享服务在两个不同ng应用程序中的控制器之间共享数据不工作

Javascript 通过共享服务在两个不同ng应用程序中的控制器之间共享数据不工作,javascript,angularjs,angularjs-scope,angularjs-service,angularjs-factory,Javascript,Angularjs,Angularjs Scope,Angularjs Service,Angularjs Factory,所以我有两个不同的html页面,两个不同的ng应用程序和两个控制器。我正在尝试在控制器和不同模块之间共享数据 下面是应用程序的基本结构 index.html -indexController.js login.html -loginController.js sharedService.js angular.module('sharedService', []).service('SharedService', function() { var SharedService; SharedSe

所以我有两个不同的html页面,两个不同的ng应用程序和两个控制器。我正在尝试在控制器和不同模块之间共享数据

下面是应用程序的基本结构

index.html -indexController.js login.html -loginController.js

sharedService.js

angular.module('sharedService', []).service('SharedService', function() {

var SharedService;

SharedService = (function() {

    function SharedService() {
        console.log('initializing');
    }

    var _data;
    SharedService.prototype.setData = function( data) {
        _data = data;
        /* method code... */
    };
    SharedService.prototype.getData = function( ) {
        return _data ;
    };
    return SharedService;

})();

if (typeof(window.angularSharedService) === 'undefined' || window.angularSharedService === null) {
    window.angularSharedService = new SharedService();
}
return window.angularSharedService;});


 angular.module("loginApp", ['sharedService'])
controller("loginCtrl",[
    'SharedService', function(SharedService){
    SharedService.setData(data);
    }

  angular.module("mainApp", ['sharedService'])
controller("someCtrl",[
    'SharedService', function(SharedService){
    console.log(SharedService.getData());
    }
问题是,由于应用程序是不同的,我指的是

<script src="sharedService.js"></script>


该服务被初始化两次。当我从loginApp设置数据时,它会设置数据,但是当我从mainApp查询数据时,它会检索未定义的数据,我怀疑这是因为该服务再次初始化,并且是sharedService的不同实例。

您是对的,该服务不会在不同html页面上的两个angular应用之间共享。您需要将要共享的数据持久化到内存以外的其他地方,例如本地存储或远程服务器

只是玩了一下。您只需将要在两个应用程序之间共享的
\u数据
放入
窗口
对象即可。像这样

angular.module('serv', [])
.factory('MyFactory', function () {
  window._data = 'default';
  return {
    setData: function (val) {
      console.log('setData() called with val == ', val);
      window._data = val;
    },
    getData: function () {
      return window._data;
    },
  }
});
请看这个plunker:


虽然这看起来有点难看。可能至少要给它命名名称空间。

为什么要将服务实例放在
窗口
对象上?为什么要将服务中的函数包装成iLife?在这种情况下,我会使用工厂,只需将共享值存储在工厂中的一个对象中,并让一些工厂方法返回或写入值