An issue arises when attempting to initialize an AngularJS global variable using a REST service

I am looking to establish a global variable called httpTimeout that will be initialized at the beginning. This variable will contain a Long value retrieved from a synchronous Rest Service call and will be utilized in various services.

(
 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;
    });
  };});

My rest service

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

How can I globally access and utilize the value of httpTimeout in other services? Thank you for your assistance.

Answer №1

If you are wondering how to execute code at the start of your application, you can refer to this helpful link: click here

Once your application has started, you have the option to use a different service to store the value you need.

Furthermore, if you wish to implement this behavior for all requests, you may find it useful to explore interceptors

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Seeking assistance with using JavaScript to filter posts from Contentful for a Next.js website

Currently, I am integrating a Next.js blog with Contentful and employing queries to display posts on the homepage. While I can easily use .filter() to feature certain posts based on a boolean value, I am struggling to figure out how to fetch posts that mat ...

AngularJS: Applying Timezone to Date Formatting

Having trouble formatting my date input to the desired timezone using angularjs 1.2.26. Here is a code example for reference: http://plnkr.co/edit/CxCqoR3Awcl1NFrCZYjx?p=preview {{'2015-07-10T12:37:08Z' | date : "MMMM d, y 'at' h:mma Z ...

Changing p tags to br tags on tinyMCE

I need assistance with using tinyMCE as an inline editor. I want it so that when a user is in edit mode and presses enter, <br /> is inserted instead of <p>. I have gone through the manual and FAQ but have not been able to achieve this successf ...

Refresh required to showcase newly created items

Currently, I am developing a straightforward tool that allows users to assign budgets to teams. This tool operates in a manner similar to a standard to-do list. The delete function functions smoothly without requiring a reload. However, the create functio ...

Tips for implementing a jQuery plugin on specific elements within an HTML page

I've been experimenting with the jQuery extension for searchable dropdowns available at this link. While I am impressed with its functionality, I'm looking to apply it selectively to specific elements on my webpage rather than all of them. Is the ...

Removing value types in select with ngOptions

When using angularjs version 1.5, the ng-option is creating values with type annotations like this <select name="type" ng-model="type" ng-options="k as v for (k, v) in Providers"> <option label="Provider 1" value="string:prov1">Provider1&l ...

What is the correct way to align labels to the right in a column layout?

I just started learning React JS and I'm using Material-UI. One issue I encountered is that when I use the column layout with an 'xs' value, the component inside the column appears aligned to the left. To align it to the right, I tried incr ...

Wavesurfer encounters difficulty generating waves due to a CROS Error caused by cookie settings

When using wavesurfer, an error occurs that states: XMLHttpRequest cannot load https://audiotemp.domain.net/RE65bbf6f0a2760184ab08b3fbf9f1d249.mp3. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http ...

To toggle between two scope variables within a view when one is not defined

In my application, I am utilizing two scope variables: $scope.banner and $scope.defaultBanner. The banner is fetched using a service, but in cases where the banner file does not exist, the variable will be empty as the service returns nothing. My goal is ...

The conditional statement does not align with my Regular Expression

I'm experiencing a peculiar issue with my regular expression matching in the code snippet provided. Even though the alert confirms a match between the strings, the if statement fails to trigger. Any insights on why this might be happening? Appreciate ...

Generating dynamic input field values using jQuery in a CodeIgniter PHP framework application

I am facing an issue where I am unable to display the value from a dynamically created input field in the page controller. Below is the jQuery code used to append the dynamic input fields: var tableRow='<tr>'; tableRow+=' ...

Having trouble with v-model not updating the data value on a dropdown in Vue.js?

When I set the initial value on the data property, the dropdown option is correctly displayed. However, if I select a different value from the dropdown, the data property does not update accordingly. <select class="form-control" ...

Angular Configuration Not Functioning Properly

After injecting an AuthService into my code, I noticed that the console.log(apiServerConstants.url) line in the factory is returning undefined. Why is this happening? angular.module("webclient.constants", []) .constant("apiServerConstants", { "url": ...

Why isn't the page showing up on my nextjs site?

I've encountered an issue while developing a web app using nextjs. The sign_up component in the pages directory is not rendering and shows up as a blank page. After investigating with Chrome extension, I found this warning message: Unhandled Runtime ...

The function is receiving an empty array of objects

This code is for an Ionic app written in typescript: let fileNames: any[] = []; fileNames = this.getFileNames("wildlife"); console.log("file names:", fileNames); this.displayFiles(fileNames); The output shows a strange result, as even though there ar ...

When making an Ajax request, the response is received successfully, however, the success, complete, and error

I am attempting to retrieve SQL results from a database using an AJAX call and display them on another PHP page. Here is my AJAX call code: function newfunc(){ start += 10; var params = parseURLParams(document.URL); var datastring = "nextStart="+start+"&a ...

React issue: parent state becoming unresponsive when updating during input onChange event

I am currently working on a large form that consists of at least 50 input fields. I have developed a function within the form component to store the input values in the form's state: PARENT FUNCTION saveToState(details) { const { company } = thi ...

Exceljs : 'An issue has been identified with certain content in the document named "file.xlsx"(...)'

I have encountered an issue with an xlsx file generated using Exceljs. While I have been creating csv files in my project without any problems, creating an xlsx file now poses an error. The xlsx file opens clean on Ubuntu/LibreOffice Calc, but there is an ...

Manipulating divs by positioning them at the top, left, right, bottom, and center to occupy the entire visible portion of the page

Many suggest avoiding the use of table layouts and opting for divs and CSS instead, which I am happy to embrace. Please forgive me for asking a basic question. I am looking to create a layout where the center content stretches out to cover the entire visi ...

Chaining asynchronous HTTP requests in Angular 2: A guide to stopping execution if one request fails

I am faced with a challenge of executing an array of HTTP requests in a specific order, where if any request fails, the subsequent ones should not be executed. Is there a way to achieve this requirement? What would be the recommended approach to hand ...