Ivan Burnaev
Ivan Burnaev

Reputation: 2730

How to use injected services in static methods

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?

Upvotes: 6

Views: 3967

Answers (1)

basarat
basarat

Reputation: 276085

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.

Upvotes: 0

Related Questions