1

I have a JS library with some functions like this:

function TestService() {
}

TestService.init = function() {
 ------
}

TestService.foo = function() {
--------------
}

Now this is an old library and would like to use these functions in angular code. Is there a way we can wrap it up as service/factory and use it properly? Appreciate help in advance.

3 Answers 3

2

If your class is designed to be instantiated with new, just pass it to .service:

app.service('testService', TestService);

Angular will call new on it once to create a singleton.

Edit:

I thought your question had an error and the functions were really supposed to be assigned to the prototype.

If you just need that function, return it using a factory or constant:

app.factory('testService', function() {
    return TestService:
});

Or:

app.constant('testService', TestService);
Sign up to request clarification or add additional context in comments.

12 Comments

didn't know angular was doing it by himself, that's pretty sweet
Yep, that's main difference between .factory and .service. When you use .factory you have to pass it a function that returns an object. When you use .service you pass it a constructor function on which it will call new.
I am using ES 6 syntax which requires class structure.
@FrankModica Didn't work for me. My function structure is as mentioned above.
It should still work. Classes are just syntax sugar.
|
0

Yes of course :

// trying to gimmick your library
function test() {}

test.prototype.hey = function() {
    alert('hey');
}

test.prototype.hoy = function() {
    alert('hoy');
}

app.factory('testFactory', function() {
    // that's where your using your library
    return new test();           
});

And then use it in controller :

// controller function
function HelloCtrl($scope, testFactory) {
    testFactory.hey();
}

try it :

http://jsfiddle.net/ufLf50n1/

2 Comments

So i have to use prototype inheritance. Can't i use it as it is and then wrap it as service and use in controllers via injection. This lib changes from time to time and we are use it through cdn.
prototype was just an exemple of possible librabry
0

You can use returning class from angular.factory:

class TestService {
  constructor() {
    console.log('instance initialized');
  }

  static init() {
    console.log('static init')
  }

  static foo() {
    console.log('static foo');
  }
}

angular.module('app', [])
  .factory('TestService', () => TestService)
  .run((TestService) => {
    new TestService();
    TestService.init();
    TestService.foo();
  })
<script src="https://code.angularjs.org/1.6.2/angular.js"></script>
<div ng-app="app"></div>

2 Comments

How does rewriting as a class help? It does not appear as though the AngularJS syntax would differ between the two...
@MikeMcCaughan not rewriting but returning. The class part itself is not that important, it's just more convenient to write in es6 style (the question tag indicates ecmascript6). What is important though is returning the library api, without calling new or whatever in service. Letting the judgement over the service usage to be commited in the controller by passing it integrally through into controller with all static props.

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.