just wondering if anyone had any patterns / approaches to doing "object oriented" services in angular. By this I mean a service that basically creates a class that creates "instances" of itself:
function MyService(injectedServiceA, injectedServiceB) {
return function MyService(arg1, arg2 ... ) {
var var1 = arg1;
this.someFunction() {
... do something with var1 ...
}
}
}
and injected:
angular.module('MyModule', []).service('MyService', MyService);
and this service could then be provided to a controller and the controller could then create an instance of it:
$scope.myService = new MyService(arg1, arg2 ... );
and the view uses someFunction.
I took this (naive?) approach which did not work:
class MyService {
{
constructor(arg1, arg2 ... ) {
this._var1 = arg1;
}
someFunction() {
... do something with this._var1 ...
}
}
}
class MyServiceMaker {
constructor(injectedServiceA, injectedServiceB);
make(arg1, arg2 ... ) { return new MyService(arg1, arg2 ...); }
}
and injected:
angular.module('MyModule', []).service('MyServiceMaker', MyServiceMaker);
and then in controller:
$scope.myService = MyServiceMaker.make(arg1, arg2 ... );
this constructed totally fine, etc. But when my someFunction is called on myService, the this was undefined.
newsyntax instead of my own stupid maker class)thismeans. How you are defining the class looks fine, your issue may be how you are using it.