Answers for "how to read json file in angular 8 using http"

0

angular load json file with httpclient

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

@Injectable()
export class AppSettingsService {

   constructor(private http: HttpClient) {
        this.getJSON().subscribe(data => {
            console.log(data);
        });
    }

    public getJSON(): Observable<any> {
        return this.http.get("./assets/mydata.json");
    }
}
Posted by: Guest on February-23-2021
0

get data from json file angular

/*--IN THE TS OF THE COMPONENT--*/
//In imports section
import variableName from '../../assets/fileName.json';

//In the class part of the same file
export class AppComponent {
  //This variable will have all the values
  public nameList:{field1:type, field2:type}[] = variableName;
}

/*--IN A NEW TS FILE--*/
//Create another js file in the app folder with
declare module "*.json" {
	const value: any;
	export default value;
}
Posted by: Guest on March-02-2021

Code answers related to "how to read json file in angular 8 using http"

Code answers related to "TypeScript"

Browse Popular Code Answers by Language