首页 > 解决方案 > 通过调用 REST 服务初始化全局变量 AngularJS 的问题

问题描述

我想在开始时创建一个全局变量(httpTimeout)初始化,包含一个由同步调用 Rest Service 返回的 Long 值,并在不同的服务中使用它

(
 function () {
'use strict';

angular
.module('module')
.factory('MyService', function (
  $http,
  $q
 ){

var service = {};

var httpTimeout = function() {
      return $http({
          method: 'GET', '.../rest/getHttpTimeOut'
        }).then(function (response) {
          return response.data;
        }).catch(function (err) {
            return 30000;
        });
  };

 service.myService1= function (Model) {
    return $http({
      method: 'POST', '..../rest/callRestService1',
      data: Model, timeout : httpTimeout
    }).then(function (response) {
      return response.data;
    });
  };

 service.myService2= function (Model) {
    return $http({
      method: 'POST', '..../rest/callRestService2',
      data: Model, timeout : httpTimeout
    }).then(function (response) {
      return response.data;
    });
  };});

我的休息服务

@RequestMapping(value = "/getHttpTimeOut", method = RequestMethod.GET)
@ResponseBody
public long getHttpTimeOutValue() {
    return timeoutValue;
}

我如何全局检索此值(httpTimeout)以用于其他服务谢谢您的帮助

标签: javascriptangularjsresthttpecmascript-5

解决方案


如果您的问题是如何在应用程序启动时做某事: 看看那个

在您的应用程序启动后,您可以使用另一个服务来存储该值。

此外,如果您想将此行为应用于所有请求,请查看拦截器


推荐阅读