0

I'm trying to figure out how I could use one JavaScript object instantiation across multiple controllers in AngularJS. Try to envisage the following scenario:

var objTemplate = new templateObject();
objTemplate.init();

app.controller('HomeController', function($scope) {

    objTemplate.slidePage();

});

Obviously the objTemplate.initialise(); doesn't work within the controller and I'm not quite sure how to do it.

EDIT:

Thanks for the answer @Atrix - it looks like the way forward will be to create a new service and move all of the functionality from the JavaScript object to it - then use the service to perform all these operations.

Cezar
  • 55,636
  • 19
  • 86
  • 87
Spencer Mark
  • 5,263
  • 9
  • 29
  • 58

1 Answers1

1

You can try using a factory in order to instantiate your object once and access it from all controllers.

For more details about factory and service and also examples check @matys84pl & @JustGoscha answers about services & factory

Community
  • 1
  • 1
Abhishek Nandi
  • 4,265
  • 1
  • 30
  • 43