使用 TypeScript 在 AngularJS 中实现 Provider

dec*_*dec 1 typescript

我尝试使用依赖于服务的 TypeScript 来实现提供者。我想我必须将这些服务注入到 get 函数中,但这在 TypeScript 中是如何完成的?

在 JavaScript 中,它是这样实现的:

angular.module('HTML5Shell.Services')
.provider('service', [
    'baseService',
    function (baseService) {

        return {
            $get: ['$rootScope',
                function ($rootScope) {

                    return {
                        method: function (param) {
                            return 'method called';
                        }
                    };
            }]
        };
    }]);
Run Code Online (Sandbox Code Playgroud)

dec*_*dec 5

我找到了解决方案 - 也许这是实现提供者所需的解决方案,除了使用任何 ;-)

module services {
    'use strict';

export class Service {

        private $rootScope:any;

        public start($rootScope:any) {
            this.$rootScope = $rootScope;
        }

        public serviceMethod() {
            this.$rootScope ...
        }
}

export class ServiceProvider implements ng.IServiceProvider {

        static $inject = ['baseService'];

        constructor(private baseService:any) {
        }

        $get = ['$rootScope', ($rootScope:any) => {
                var translationService = new Service();
                translationService.start($rootScope);
                return translationService;
            }];

        public configure = () => {
            this.baseService.method();
        }
}

    angular
        .module('Module')
        .provider('service', ServiceProvider);
}
Run Code Online (Sandbox Code Playgroud)