TypeScript AngularJS将一个角度模块注入另一个角度模块

时间:2015-10-16 14:39:47

标签: angularjs typescript

这是我的一个主要角度模块,其上定义了常量。

/// <reference path="../../../thirdparty/angular/angular.d.ts"/>
var FeatureFlagsValues: ng.IModule = angular.module('feature-flags.values', []);
 FeatureFlagsValues.constant('FeatureFlagValues', {});
 FeatureFlagsValues.run(['$q', '$http', 'FeatureFlagValues', function (_q:ng.IQService, _http: ng.IHttpService, FeatureFlagValues: any) {
let deferred = _q.defer();
let uri = "/api/v1/features/user";

_http.get(uri)
    .success(function (data) {
        deferred.resolve(data);
    })
    .error(function (data, status) {
        deferred.reject({data,status});
    });

deferred.promise.then((results: any) => {
    angular.forEach(results.EnabledFlags, function (key) {
        FeatureFlagValues[key] = true;
    });
});
deferred = _q.defer();
uri = '/api/v1/features/user?defaults=true';

_http.get(uri)
    .success(function (data) {
        deferred.resolve(data);
    })
    .error(function (data, status) {
        deferred.reject({ data, status });
    });
deferred.promise.then((defaultResults: any) => {
    angular.forEach(defaultResults.EnabledFlags, function (key) {
        FeatureFlagValues[key] = true;
    });
 });

}]);

这是我的另一个角度模块,我想在其中使用前一个模块中定义的常量,我不知道我该怎么做。我做的研究找不到任何好的例子。

 // <reference path="./FeatureFlags.values.ts"/>
 import {Endpoints as endpoints} from '../../endpointsTsModule';
 import {FeatureFalgs as FF}  from './FeatureFlags.service.SettingStore';

 export module FeatureFlags.service {
 export class FeatureFlagsService {
 static $inject: string[] = ["$q", "$http", "feature-flags.values","FeatureFlagsSettingStore"];
   }
}

1 个答案:

答案 0 :(得分:0)

如果您想使用&#39; FeatureFlagValues&#39;你需要制作&#39; feature-flags.values&#39;模块&#34;另一个角度模块的依赖关系&#34;你指的是。像这样:

angular.module('another', ['feature-flags.values']);

然后您可以通过注入它来在您的服务中访问它:

 export class FeatureFlagsService {
     static $inject: string[] = ["$q", "$http", "FeatureFlagValues","FeatureFlagsSettingStore"];
   }