1

When i tried to access the local Json data in angular 5 using httpclient was not able to fetch the data but when tried to access the same with the web server it is working. i have added the api folder in the angular-cli.json file and still not able to access the json.

employee.component.ts

    import {Component} from "@angular/core"

    import {EmployeeService} from './employee.service'

@Component({
  selector:'app-employee',
  templateUrl:'./employee.component.html'
})

export class EmployeeComponent {
         list:any
        data:any
   constructor(private employee:EmployeeService){}
     ngOnInit() {
       this.employee.getEmployee().subscribe(list=>{
      this.data= this.list.data
       console.log(this.list)
       })

     }
}

employee.service.ts

import {Injectable} from "@angular/core";
import {HttpClient} from "@angular/common/http";
import "rxjs/add/operator/map";

@Injectable()

export class EmployeeService {
    url: string = "src/app/api/employee.json"
    constructor(private http: HttpClient) {}

    getEmployee() {
       return this.http.get(this.url)
    }
}

employee.json

{
  "data": [
    {
      "id": 66896,
      "name":"Vinay",
      "phone": "9620529048",

      "address":{ 
        "city":"Bangalore",
        "street":"ABC street",
        "state":"Karnataka",
        "PostCode":"560076"
    }
    },
    {
      "id": 66923,
      "name":"John",
      "phone": "7720808609",

      "address":{ 
        "city":"Hyderabad",
        "street":"PQR street",
        "state":"UttarPradesh",
        "PostCode":"412114"
    }
    },
    {
      "id": 66898,
      "name":"Vijay",
      "phone": "8888830456",

      "address":{ 
        "city":"Pune",
        "street":"XYZ street",
        "state":"Maharastra",
        "PostCode":"411067"
    }
    }
  ]
}

path for json file - src->app->api->employee.json

Can someone share their knowledge so that i can solve this issue

please find my angular-cli.json file

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "demo": {
      "root": "",
      "sourceRoot": "src",
      "projectType": "application",
      "prefix": "app",
      "schematics": {},
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/demo",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.app.json",
            "assets": [
              "src/app/api/employee.json",
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.css"
            ],
            "scripts": []
          },
          "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "aot": true,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true
            }
          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "demo:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "demo:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "demo:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.spec.json",
            "karmaConfig": "src/karma.conf.js",
            "styles": [
              "styles.css"
            ],
            "scripts": [],
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ]
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "src/tsconfig.app.json",
              "src/tsconfig.spec.json"
            ],
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    },
    "demo-e2e": {
      "root": "e2e/",
      "projectType": "application",
      "architect": {
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "demo:serve"
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": "e2e/tsconfig.e2e.json",
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    }
  },
  "defaultProject": "demo"
}

1 Answer 1

1

Look at the angular-in-memory-web-api, it serves to simulate a data server, it intercepts HTTP requests. Angular has done a great tutorial : https://angular.io/tutorial/toh-pt6

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.