问题
I am trying to implement internationalization for my web application by using ngx-translate . I am able to access JSON data in the HTMl file but not able to access JSON data in the angular(.ts) file. Can anyone suggest me how to access it in .ts file.
回答1:
You get the input from json files like you call a backend service. The syntax is the same, but if you read from a local json file it is an synchronous call.
// Import HTTP Client
import { HttpClient } from '@angular/common/http';
// Define property
myDataObject: any;
// Dependency Injection
constructor(private http: HttpClient) {}
// Get the data
ngOnInit() {
this.myDataObject = this.http.get<any>("assets/json/data.json")
.map(response => response)
}
Now you can get the properties from you json object like:
this.myDataObject.myProperty
来源:https://stackoverflow.com/questions/49125298/not-able-to-access-json-data-into-angular-ts-file