How can I define an AngularJS factory using TypeScript class that has constructor parameters

前端 未结 6 845
北恋
北恋 2020-12-30 01:53

I want to write a TypeScript class that gets a \"prefix\" parameter in the constructor, this class also needs access to a LogService inject.

Using plain JavaScript y

6条回答
  •  离开以前
    2020-12-30 02:36

    I have achieved like below

    module Dashboard {
        export class LayoutServiceFactory {
            static $inject = ["$q", "$http"];
            private q: ng.IQService;
            private http: ng.IHttpService;
    
            constructor(private $q: ng.IQService, private $http: ng.IHttpService) {
                this.q = $q;
                this.http = $http;
            }
    
            getDataFromServer(serviceUrl) {
                var deferred = this.q.defer();
                this.http.get(serviceUrl, null)
                    .then(response => {
    
                        deferred.resolve((response) as any);
    
                    });
                return deferred.promise;
            }
    
            static factory() {
                var instance = ($q: ng.IQService, $http: ng.IHttpService) =>
                    new LayoutServiceFactory($q, $http);
                return instance;
            }
        }
    
        appModule.factory("LayoutService", LayoutServiceFactory.factory());
    }
    

提交回复
热议问题