6

I want to use angularjs and typescript together. I'm trying to create Orm factory with typescript and stacked with some problem.

I defined my factory class as:

class OrmModel implements IOrmModel {
    static $inject = ['$http', '$q', 'config'];

    private name:string;
    private isNewRecord:boolean = false;

    constructor(public $http:ng.IHttpService, private $q:ng.IQService, private config:Object) {
        //...
    }

    static findAll(params:ISearchParams, relations:string[]):ng.IPromise<OrmModel> {
        //...
    }
}

Here I defined factory.

OrmModule:ng.IModel = angular.module('core.orm', []);
OrmModule.factory('OrmModel', ['$http', '$q', OrmModel]);

How can I use $http or $q in findAll() method?

Ivan Burnaev
  • 2,690
  • 18
  • 27
  • Notice: with a class, you should use `myModule.service('OrmModel', OrmModel)`. – Paleo Apr 03 '15 at 13:23
  • Using a dependency from a static function is a bad idea. However, you can declare a static variable `private static $http;`, then, in the constructor, do : `OrmModel.$http = $http;` – Paleo Apr 03 '15 at 13:26
  • I think, I should use `service` like this `myModule.service('OrmModel', new OrmModel())`, because services in angular most of all are singletons – Ivan Burnaev Apr 03 '15 at 13:27

1 Answers1

1

To live in the angular ecosystem singletons should be services. So move the findAll function into its own service. That way it can have access to other services like $http and $q.

basarat
  • 261,912
  • 58
  • 460
  • 511
  • You don't recommend working with singletones even if we are coding typescript? Isn't there any way to get access to injected parameter while working with typescript singletones? – iberbeu Mar 31 '16 at 13:09