What is the best way to access a value from a settings.json file in an Angular .ts file?

I am working on implementing debounceTime to allow the user to finish typing before suggestions are generated. I want to give the user the ability to customize how much time is given before suggestions appear. To achieve this, I have added a configuration option in my settings.json file:

{
  "EN": {
    "workbaskets": {
      "time": {
        "debounceTime": 750
      },

In addition, I have defined an interface in my settings.ts file for exporting it:

import { map } from 'rxjs/operators';
import { OperatorFunction } from 'rxjs';

export interface Customisation {
  [language: string]: CustomisationContent;
}

export interface CustomisationContent {
  workbaskets?: WorkbasketsCustomisation;
  classifications?: ClassificationsCustomisation;
  tasks?: TasksCustomisation;
}

export interface WorkbasketsCustomisation {
  information?: { owner: LookupField } & CustomFields;
  time?: DebounceTime;
  'access-items'?: AccessItemsCustomisation;
}

export interface DebounceTime {
  debounceTime: number;
}

Now I am facing difficulty assigning the value to debounceTime. In my ngOnInit method, I have tried the following approach (only showing relevant parts of the code):

this.time = this.httpClient.get<DebounceTime>(customisationUrl).pipe(map((data) => {
      return data.debounceTime;
      })
    );

debounceTime(this.time)

I have declared my variable as time: number = 0; outside of the ngOnInit method. However, this results in an error: Type 'Observable' is not assignable to type 'number'. As a newcomer to Angular, I am still learning and struggling to resolve this issue. I have attempted to find solutions by researching existing questions, but with no success. Any guidance or assistance would be greatly appreciated. Since the application already utilizes settings.json and settings.ts, I prefer to continue using this approach for configuring debounceTime and simply need help on how to pass the value as input to the debounceTime function.

Answer №1

 this.httpClient.get<DebounceTime>(customisationUrl).pipe(map((data) => {
  
  }).subscribe((response)=>{
       this.time = data.debounceTime;
 });

It is important to note that the map operator in this code snippet is used for modifying data and not returning it. The actual assignment of data to a local class property is done within the subscribe method. If you have any further inquiries, feel free to reach out.

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

What is the best way to transmit an i18n key from a parent component to a child component prior to beginning

When working with an input-field component that can be embedded in various parent components using forms, I encountered a challenge. Within the input child component, there is an i18n translation key variable using interpolation, which I aim to dynamicall ...

Tips for managing various potential return types in TypeScript?

In my research on the topic, I came across a discussion thread about obtaining the local IP address in Node.js at Get local IP address in Node.js. In that thread, there is a code snippet that I would like to incorporate: import net from 'net'; c ...

Exploring array data retrieval in Angular

I need to display various inch sizes for different bike models. I have a json file containing an array of bike data. [{ "name": "Mountainbike", "img_url": "assets/img/mountainbike.jpg", "mount_size": [{&quo ...

What is the method to invoke a function within another function in Angular 9?

Illustration ` function1(){ ------- main function execution function2(){ ------child function execution } } ` I must invoke function2 in TypeScript ...

What is the best approach for presenting multiple entries with identical IDs in a table using Angular?

I obtained a JSON response in the following structure; https://jsonplaceholder.typicode.com/posts My goal is to format the table to resemble the attached image structure. However, my current implementation is displaying repeating user IDs. How can I adju ...

Forward slashes in JSON output from Node.js

I recently encountered an issue with receiving JSON data from a Node server that contained unnecessary slashes, making it difficult to parse. The JSON data looked like this: "{\"responseCode\":200,\"headers\":{\"Access-Control-All ...

Step-by-step guide on making a tab the default tab in Angular with the help of Angular Material Tabs

I recently created an Angular application that utilizes the Grid and Tab components from angular material. One unique feature of my app is the data binding I implemented between the grid and tabs, where the number of grid values corresponds to the number ...

Revitalize access token with Keycloak in Javascript

I am currently working with keycloak-js version 8.0.1 and have a function called getToken that checks if the token is expired. If it is expired, the function refreshes it; otherwise, it returns the current token. The issue I am facing is that even though t ...

Removing buttons from a table row dynamically

Below is how I am adding the Button to Element: (this.sample as any).element.addEventListener("mouseover", function (e) { if ((e.target as HTMLElement).classList.contains("e-rowcell")) { let ele: Element = e.target as Element; let ro ...

Routing in Angular 2: Find all paths beginning with a specific prefix

I am trying to configure the Angular router to match any path that starts with a specific prefix and route it to a component. Below is my current router setup: { path: 'posts/:id/:title', component: PostDetailsComponent } Everything ...

Styling with Css Attributes and Text Formatting Styles

I am working with a custom textStyle that is of type TextStyle, coming from react-native types, or React.CSSProperties. I want to apply this styling to an html span element using the style attribute. The style attribute only accepts types of React.CSSPrope ...

Angular HTTP POST not triggering the desired action

Having issues with a simple call from my Angular app to an ASP.NET Core WebAPI. Here's the code snippet: this.http.post(actionUrl, credentialsDto) .map((data: Response) => { if (data !== null) { localStorage.setItem(this.tokenStorageKe ...

Node.js is throwing a JSON parsing error due to an unexpected end of input

Currently, I am attempting to analyze the JSON data that is returned using the following PHP code snippet; $UserData = $con->query("SELECT * FROM Discord WHERE RobloxID=".$UserId) or trigger_error($mysqli->error); $result = $UserData->fetch_a ...

What is the proper way to configure QuoteName settings in Json.Net?

Initially, I am aware that the JSON format supplied is not valid. However, due to my customer web service configuration requirements, I am in need of setting QuoteName of Json.Net JsonTextWriter to false. Unfortunately, I am uncertain of the correct method ...

Struggle with Loading Custom Templates in Text Editor (TinyMCE) using Angular Resolver

My goal is to incorporate dynamic templates into my tinyMCE setup before it loads, allowing users to save and use their own templates within the editor. I have attempted to achieve this by using a resolver, but encountered issues with the editor not loadin ...

What is the procedure for creating a JSON object in Android using Java?

Below is the code snippet I am working with: { "body": "{\"data\": [[1633209578,0,117.000000],[1633209578,1,116.000000],[1633209624,2,121.000000],[1633209643,3,174.000000],[1633209682,4,222.000000],[1633209715,5,247.0 ...

The risk of a race condition could arise when working with nested switchMaps in ngr

I am currently working on an Angular 9 application that heavily relies on observables. In a specific component, I have the following requirements: Retrieve all companies to access certain information. Fetch all responses and link additional company detai ...

Utilizing JSON for sending model data to a controller in ASP.NET Core

I'm a beginner with ASP.NET Core and I'm attempting to send a view model to my controller. Data Model: public class ToolModel { public ToolModel() { } public string text { get; set; } public string type { get; set; } public stri ...

A step-by-step guide on accessing .json files from the Windows file explorer using Vue.js

I searched for a way to access a .json file without storing it in my code base, but all the posts I found assumed that the file would be saved within the code. They provided solutions like this one: How to acces external json file objects in vue.js app, wh ...

How to properly handle Angular routing errors and best practices?

Currently, I have been delving into learning Angular to integrate with my Ruby on Rails application. However, I have encountered some challenges specifically related to routing. Here is a snippet from my app.routing file: import { NgModule } from '@ ...