Not able to access JSON data into angular(.ts) file

二次信任 提交于 2019-12-24 07:13:35

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!