2

How to read key values from appsettings.json to typescript file using angular4

{
  "Logging": {
    "IncludeScopes": false,
    "Debug": {
      "LogLevel": {
        "Default": "Warning"
      }
    },
    "Console": {
      "LogLevel": {
        "Default": "Warning"
      }
    }
  },
  "Application": {
    "ServiceUrl": "http://localhost:12345",
    "BaseURL": ""
  },
0

1 Answer 1

2

Depending on where this JSON file is located in your Web application, you would be able to use Angular's Http client to do a GET request and fetch it's data. You can even add a typescript interface to expect a result. A bit like this:

import { Observable } from 'rxjs/Observable';
import { HttpClient } from '@angular/common/http';

export interface someInterface {
   foo: string,
   bar: number
}

export class SomeClass {
  constructor(private http: HttpClient) { }
  data: someInterface;

  someFunc(){
    this.http.get<someInterface>('/path/to/json').subscribe(result => {
       this.data = result;
    });
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks,this is more useful for me.Where I need to call this someFunc() method to get all the variables before starting my app or before starting other functions

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.