在项目启动时从$http设置Angularjs常量.获取

Set Angularjs constants at project startup from $http.GET

本文关键字:设置 Angularjs 常量 获取 http 项目 启动      更新时间:2023-09-26

在我的应用程序中,我有一个业务流程服务,该服务从向其注册的不同服务中获取uri。 service_1service_2可能位于不同的计算机上,但其中一台已注册,其计算机的 URI 将被存储。

在使用该业务流程服务的其他应用程序中,我想调用业务流程服务以获取要使用的 uri,但随后我想将它们设置为 Angular 常量,或者至少能够使用 uri 的值。

因此,这是将使用"常量"的服务,

"常量"是从编排服务中提取的 uri:

(function () {
    'use strict';
    angular
        .module('data.model-view', ['restapi'])
        .factory('MVService', MVService);
    MVService.$inject = ['$http', '$q', 'exception', 'logger', 'restapi'];
    /* @ngInject */
    function MVService($http, $q, exception, logger, restapi) {
        var HOST = restapi.mvservice.HOST;
        var MODULE = restapi.mvservice.MODULE;
    ...
    //below is an example of what will use the above host/module in order to 
    //get the data needed for this application
    function getModels() {
        return $http.get(HOST + MODULE + '/model/retrieveAll')
            .then(success)
            .catch(fail);
        function success(response) {
            return response.data;
        }
        function fail(e) {
            return exception.catcher('XHR Failed for retrieveAll')(e);
        }
    }
因此,这是我希望在其中

设置常量的restapi模块,以便我可以在整个应用程序中访问它们,但我需要首先从业务流程服务获取它们。

(function() {
    'use strict';
    var data = '';
    angular
        .module('restapi', [])
        .factory('restapi', function($http, exception) {
            var HOST = %%ORCSERVICE%%;
            var MODULE = '/orchestration/service/rest/data';
            return $http.get(HOST + MODULE)
            .then(success)
            .catch(fail);
            function success(response) {
                //so at this point I can actually access the data I need
                //with a console.debug(response.data);
                return response.data;
            }
            function fail(e) {
                return exception.catcher('XHR Failed to reach orc')(e);
            }
        }).constant('restapi', constant());
    function constant() {
        //set constants here based on the data pulled from above
       //ideally I want the result of this to be like
       //{
       //    mvservice: {
       //        'HOST': 'http://xxxxxxxxxx.amazonaws.com'
       //        'MODULE': '/rest/service/whatever'
       //    },
       //    ... //other service here
       //}
    }
})();

就像我在上面的评论中所说的那样,我实际上可以从上面的$http.get中获取我需要的数据(uris)。然后,我只是希望能够将数据集作为常量获取,或者至少以我可以访问它的形式获取。因为当MVService启动时,它需要业务流程服务中自己的 uir 才能进行其余调用。抱歉可能有点混乱,如果需要澄清,请告诉我。

获取必要的数据后尝试引导应用程序:

var injector = angular.injector(['ng']),
    http = injector.get('$http');
http.get(HOST + MODULE).then(function (result) {
    app.value('restapi', result.data);
    angular.bootstrap(document, [app.name]);
});

有多种方式:

如果你想使用 angular.constant,你可以通过延迟 angular 的引导程序来获取 url,直到你得到你的值。因为一旦加载了 ng-app,就无法设置常量。要做到这一点,请参阅卡拉克苏纳的答案。

另一种方法是在服务的构造函数中执行角度查询。因为在解析服务实例化阶段的所有承诺之前,控制器的路由和调用不会发生。因此,您可以将请求的结果存储为服务的字段,然后在应用程序的控制器/指令/angular.run 部分上访问它,而不会出现任何赛车问题。