Guide to retrieving Azure web app application settings in a TypeScript file

I recently created an Angular 2 application using Visual Studio 2015. After that, I successfully published my Angular 2 web app to Azure Web App and everything seems to be working fine. However, I am facing a challenge in accessing the application setting values from Azure Web App into my TypeScript file for the Angular 2 web app.

Could someone kindly guide me on how to retrieve the azure web app application settings values within any TypeScript file?

-Pradeep

Answer №1

The issue mentioned above was successfully resolved by accessing the Azure web app application setting values in a TypeScript file.

To achieve this, I added the following code to Index.cshtml in order to access the app setting values from the web.config file:

    <script>
    window.config = {
        apiBaseUrl: '@System.Configuration.ConfigurationManager.AppSettings["ApiBaseUrl"]',

    };
</script>

Subsequently, I included the below code in Services.ts:

import {Injectable, Inject, Optional, OpaqueToken} from '@angular/core';
export const API_BASE_URL = new OpaqueToken('API_BASE_URL');

@Injectable()
export class ApplicationClient {
private baseUrl: string = undefined; 

constructor(@Optional() @Inject(API_BASE_URL) baseUrl?: string) {
    this.baseUrl = baseUrl ? baseUrl : ""; 
  }
}

Next, I implemented the code below in app.module.ts:

import { ApplicationClient, API_BASE_URL } from './shared/Services';
providers: [{ provide: API_BASE_URL, useValue: window['config']['apiBaseUrl'] }]

Finally, wherever the setting value needs to be accessed, just import the path for Services.ts file in your TypeScript file like so:

import { API_BASE_URL } from '../shared/Services';
 @Injectable()
export class writingService {
private _AppUrl = API_BASE_URL+'api/myApplication/';
}

-Pradeep

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

Setting up a Mean Stack on AWS Lightsail

I am currently experimenting with AWS Lightsail and struggling to grasp how to set it up properly. I have successfully created a Bitnami MEAN instance. On my local machine, I have Angular 6 running via CLI and a NODE API backend on two different ports: 42 ...

Pause and be patient while in the function that delivers an observable

I have a function that loads user details and returns an observable. This function is called from multiple places, but I want to prevent redundant calls by waiting until the result is loaded after the first call. Can anyone suggest how this can be accompli ...

Is there a different term I can use instead of 'any' when specifying an object type in Typescript?

class ResistorColor { private colors: string[] public colorValues: {grey: number, white: number} = { grey: 8, white: 9 } } We can replace 'any' with a specific type to ensure proper typing in Typescript. How do we assign correct ...

Adding the activateRoute class to Angular for enhanced functionality

My question pertains to a specific section in the book pro-Angular-6, where I encountered the following syntax: constructor(private model:Model,activatedRoute:ActivatedRoute) {} I am unsure about the following aspects: How can we use a class without i ...

Unable to assign a value to an undefined property in TypeScript

I need to store data in an object and then add it to another object let globalSamples = {} as any; let sample = { } as ISamplesDetail []; sample = []; for (let i = 0 ; i<this.prelevementLingette.samplesDetail.length; i++) { sample [i].id= thi ...

What is the reasoning behind ethers.js choosing to have the return value of a function be an array that contains the value, rather than just the value itself

An issue arose with the test case below: it('should access MAX_COUNT', async () => { const maxCount = await myContract.functions.MAX_COUNT(); expect(maxCount).to.equal(64); }); The test failed with this error message: ...

What is the method to have VIM recognize backticks as quotes?

Currently working in TypeScript, I am hoping to utilize commands such as ciq for modifying the inner content of a template literal. However, it appears that the q component of the command only recognizes single and double quotation marks as acceptable ch ...

Migration of old AngularJS to TypeScript in require.js does not recognize import statements

I am looking to transition my aging AngularJS application from JavaScript to TypeScript. To load the necessary components, I am currently utilizing require.js. In order to maintain compatibility with scripts that do not use require.js, I have opted for usi ...

What is the best way to encapsulate a slider within a fragment to prevent the occurrence of the "Type 'Element[]' is not assignable to type 'ReactNode'" error?

I'm encountering an issue with a Slider component in a NextJs landing page template. Every time I try to map through an array within the Slider component, I receive an error. I've attempted to find solutions online and came across this related th ...

The Angular HTML component is failing to display the locally stored JSON data upon page initialization

import { Store,STORES } from '../models/store'; export class StoreLocatorComponent implements OnInit { public Stores: any = []; constructor() { } ngOnInit(): void { this.Stores = STORES ; this.Stores.forEach(element => { ...

Anticipating the completion of multiple observable subscription functions

Is there a way to replace and convert all words in an array using an object's method that returns an observable? I found a helpful solution on this post which uses bind to pass the correct value. After all subscriptions are complete, I want to execut ...

Creating non-changing identifiers with ever-changing values in Angular by leveraging TypeScript?

I need to transform all the labels in my application into label constants. Some parts of the HTML contain dynamic content, such as This label has '1' dynamic values, where the '1' can vary based on the component or a different applicat ...

Encountered an issue when attempting to include the "for" attribute to a label within an angular 2.0 template

I am currently using Angular 2.0 framework and working on developing an input component. In addition to that, I am also utilizing Google MDL which requires a specific HTML structure with labels for inputs. However, when trying to implement this, I encounte ...

Bringing in Chai with Typescript

Currently attempting to incorporate chai into my typescript project. The javascript example for Chai is as follows: var should = require('chai').should(); I have downloaded the type definition using the command: tsd install chai After refere ...

looking to restrict the interface to only specific types

Currently working with TypeScript and I have a query regarding the utilization of TypeScript interface. Is there a way to selectively extract and output specific key types from the interface being used? While I am able to isolate the type I need, I am inte ...

Mocking Dependencies in TypeScript with Jest

Here is the content of my index.ts file: import {IS3Client, S3Client} from './client/S3Client'; const s3: IS3Client = new S3Client(); export async function someFunc(event: any, context: any, callback: any) { const x: string = await s3.getFil ...

Is it acceptable to use Angular to poll data from Laravel?

I have created a platform where users can stay updated with notifications and messages without the need to constantly refresh the page. Instead of using third-party services like Pusher, I decided to utilize polling to achieve this functionality. Essentia ...

There is no overload that fits the current call | Typescript, React, and Material UI

Embarking on my TypeScript journey with React and Material UI, I am hitting a roadblock with my initial component. // material import { Box } from '@material-ui/core'; // ---------------------------------------------------------------------- ...

Troubleshooting: Resolving JSX props issue in Vue template

Ever since integrating the FullCalendar library into my Vue project, I've been encountering an error every time I try to use my custom component in a Vue template. My setup includes Vue 3, Vite, VSCode, eslint, and prettier. This issue seems to be m ...

The date displayed in the table is incorrectly showing 04 instead of 03 when using the pipe

{{element.createdAt | date: 'MM/dd/yyyy h:mm'}} why are the dates in the database all showing as 23 but some values are displaying as 24? Please confirm. The first two values created in the db show a createdAt time of 3, but the table is showing ...