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?

3
  • Notice: with a class, you should use myModule.service('OrmModel', OrmModel). Commented Apr 3, 2015 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; Commented Apr 3, 2015 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 Commented Apr 3, 2015 at 13:27

1 Answer 1

0

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.

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

1 Comment

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?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.