Is it possible to share data between two angularjs apps?

后端 未结 1 1840
旧巷少年郎
旧巷少年郎 2020-12-29 08:29

I have two different apps on the same page.

Can I share data between these two apps via a service (or any other way?)?

Or is this not possible?

(my s

相关标签:
1条回答
  • 2020-12-29 09:23

    I eventually solved it in the following manner:

    angular.module('sharedService', []).factory('SharedService', function() {
    
      var SharedService;
    
      SharedService = (function() {
    
        function SharedService() {
          /* method code... */
        }
    
        SharedService.prototype.setData = function(name, data) {
          /* method code... */
        };
        return SharedService;
    
      })();
    
      if (typeof(window.angularSharedService) === 'undefined' || window.angularSharedService === null) {
        window.angularSharedService = new SharedService();
      }
      return window.angularSharedService;});
      /* now you can share the service data between two apps */
      angular.module("app1", ['sharedService'])
        /* module code */
      angular.module("app2", ['sharedService'])
        /* module code */
    

    I have to admit the fact that this solution is not ideal - but this is the cleanest solution I found for this problem.

    0 讨论(0)
提交回复
热议问题