# Share services from plugins

**URL:** <https://discuss.elastic.co/t/share-services-from-plugins/76777>\
**Category:** Kibana\
**Created:** [February 28, 2017, 12:50pm UTC](https://discuss.elastic.co/t/share-services-from-plugins/76777 "2017-02-28T12:50:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![valereds](https://avatars.discourse-cdn.com/v4/letter/v/f19dbf/32.png) [@valereds](https://discuss.elastic.co/u/valereds)\
**Post date:** [February 28, 2017, 12:50pm UTC](https://discuss.elastic.co/t/share-services-from-plugins/76777/1 "2017-02-28T12:50:30Z")

</div>

Hi all I've one app plugin and one vis plugin, and I want to share an angularjs service that stores a variables and give the possibility to get or set it.  
I can't achive this, seems that the service I've created is not a singleton or during routing it is resetted.  
This is the code of service during deploy:

```
'use strict';

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }

var _uiModules = require('ui/modules');

var _uiModules2 = _interopRequireDefault(_uiModules);

_uiModules2['default'].get('app/report').service('imageService', function () {

  var imagePath = '';

  this.set = function (path) {
    imagePath = path;
  };

  this.get = function () {
    return imagePath;
  };
});
```

---

<div class="post-metadata">

**Author:** ![Brandon\_Kobel](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/brandon_kobel/32/14829_2.png) [@Brandon\_Kobel](https://discuss.elastic.co/u/Brandon_Kobel)\
**Post date:** [February 28, 2017, 11:40pm UTC](https://discuss.elastic.co/t/share-services-from-plugins/76777/2 "2017-02-28T23:40:00Z")

</div>

@valereds we generally don't store state in AngularJS services, as normal things like page refreshes will cause these to be lost. I'd recommend looking at how some of the core\_plugins use AppState to track state.

---

<div class="post-metadata">

**Author:** ![Kikketer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kikketer/32/13813_2.png) [@Kikketer](https://discuss.elastic.co/u/Kikketer)\
**Post date:** [March 1, 2017, 12:37pm UTC](https://discuss.elastic.co/t/share-services-from-plugins/76777/3 "2017-03-01T12:37:29Z")

</div>

I'd also avoid this as well since in theory people could install one plugin and not the other. I like to imagine each plugin being it's own fully enclosed piece.

Now that's not to say that we could get creative with the codebases of each plugin, where a shared library is perhaps brought in or used during build. A very simple solution is using `npm` and publishing a reusable library that both plugins depend on.

---

<div class="post-metadata">

**Author:** ![valereds](https://avatars.discourse-cdn.com/v4/letter/v/f19dbf/32.png) [@valereds](https://discuss.elastic.co/u/valereds)\
**Post date:** [March 1, 2017, 3:35pm UTC](https://discuss.elastic.co/t/share-services-from-plugins/76777/4 "2017-03-01T15:35:19Z")

</div>

Maybe the solution to create a shared libraries is the best way to get wat I want.  
Thank you all 🙂

---

<div class="post-metadata">

**Author:** ![spalger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spalger/32/14092_2.png) [@spalger](https://discuss.elastic.co/u/spalger)\
**Post date:** [March 1, 2017, 7:31pm UTC](https://discuss.elastic.co/t/share-services-from-plugins/76777/5 "2017-03-01T19:31:11Z")

</div>

Session storage can also be a useful place to put things [https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage](https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 29, 2017, 7:31pm UTC](https://discuss.elastic.co/t/share-services-from-plugins/76777/6 "2017-03-29T19:31:26Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
