0

I'm trying to access input data that's stored inside of a .json file but for some reason my path isn't recognised:

This method runs when data is requested:

    getData() {
  console.log('this is working');
  this.http.get('src/app/inputs/inputs.json')
  .subscribe(
    (data: any) => {
      console.log('this is working');
      console.log(data.text());
     }
   );
  }
}

This is my folder structure:

enter image description here

I am able to add the data whenever a separate button is clicked, just retrieving it is where I get stuck.

3
  • use ng-resource Commented Jun 11, 2018 at 11:14
  • How do I do this specifically? Commented Jun 11, 2018 at 11:15
  • 1
    Possible duplicate of Angular 5 Service to read local .json file Commented Jun 11, 2018 at 11:15

2 Answers 2

3

I think you have to add your inputs folder in the assets section of angular.json eg

"assets": [
  "src/assets",
  "src/inputs",
  "src/favicon.ico"
]

then access your data through

this.http.get('inputs/inputs.json')
Sign up to request clarification or add additional context in comments.

1 Comment

I'd asked to moved .json to assets since it is going to be static asset ;) +1
0

Try this.http.get('./inputs/inputs.json')

3 Comments

Which file contains this function?
app.component.ts
If you're using angular-cli, you must put your file.json in the assets/folder. Angular-cli cleate a index.html, style.css and severals .js and copy all the files in folder assets

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.