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.

2
  • 2
    Why wouldn't it work in the controller? Commented Jul 31, 2013 at 9:50
  • Not sure why - but it doesn't. Commented Jul 31, 2013 at 9:55

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

added links to other answers which have more details

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.