Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to share data between two angularjs apps?

Tags:

angularjs

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 suspicion is that this is not possible though regular angular mechanisms) - but I thought it would still be worth to ask...

This can be done using the window variable - but I want to avoid doing so.

Thanks!

like image 717
Rubinsh Avatar asked Apr 30 '13 13:04

Rubinsh


1 Answers

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.

like image 192
Rubinsh Avatar answered Sep 22 '22 21:09

Rubinsh