gpt4 book ai didi

javascript - 在项目启动时从 $http.GET 设置 Angularjs 常量

转载 作者:行者123 更新时间:2023-11-30 08:32:11 25 4
gpt4 key购买 nike

在我的应用程序中,我有一个编排服务,该服务从向其注册的不同服务获取 uris。 service_1service_2 可以在不同的机器上,但注册了一个,他们机器的 uri 将被存储。

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

所以这是将要使用“常量”的服务,它是从编排服务中提取的 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]);
});

关于javascript - 在项目启动时从 $http.GET 设置 Angularjs 常量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36064420/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com