2

I'm new to Angular and trying to figure out how to inject some external objects into Angular.

I have a JS script defining a global Payment object with some methods on it. I'd like to somehow import it (the Payment object) into an Angular service (from what I understood the service would be the place to that). How exactly does one go about that?

What I'd like to achieve in the end would be something like:

app.controller("myController", [PaymentService, function (payment) {....}]);
2
  • 1
    Why do you want to add it as a dependency in the first place, if its already a global JS object? Commented Aug 10, 2014 at 17:17
  • @Pr0gr4mm3r testing comes to mind... Commented Aug 10, 2014 at 17:18

2 Answers 2

3

This would work :

app.factory('PaymentService',['$window', function($window){ 
  return $window.Payment;
}]);

Using a factory is an occasion to add additional behavior, for example adding methods or checking that the global object is defined.

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

Comments

0

You can declare it either as a value:

 angular.module('foo').value('PaymentService',window.Payment);

or as a constant:

 angular.constant('foo').value('PaymentService',window.Payment);

2 Comments

You must mean window.Payment, not PaymentService.
Indeed, fixed. Thanks.

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.