david d
david d

Reputation: 43

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

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": ""
  },

Upvotes: 2

Views: 5696

Answers (1)

Zeptile
Zeptile

Reputation: 46

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

Upvotes: 2

Related Questions