How to Display Data from Json File in Angular step by step
Displaying data from a JSON file in an Angular application involves several steps. Below is a step-by-step guide to help you achieve this:
Step 1: Set Up Angular Project
If you don’t have Angular CLI installed, install it using the following command:
npm install -g @angular/cli
Create a new Angular project:
ng new your-project-name cd your-project-name
Step 2: Create JSON File
Create a JSON file (e.g., data.json) with some sample data. Place it in the “src/assets” folder.
// data.json [ { "id": 1, "name": "Item 1", "description": "Description for Item 1" }, { "id": 2, "name": "Item 2", "description": "Description for Item 2" }, // Add more items as needed ]
Step 3: Create Angular Service
Create an Angular service to fetch data from the JSON file. Generate a service using the Angular CLI:
ng generate service data
Modify the service file (‘src/app/data.service.ts’) to read the JSON file:
// data.service.ts import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { Observable } from 'rxjs'; @Injectable({ providedIn: 'root' }) export class DataService { private dataUrl = 'assets/data.json'; constructor(private http: HttpClient) {} getData(): Observable{ return this.http.get (this.dataUrl); } }
Step 4: Fetch and Display Data in a Component
Generate a component using the Angular CLI:
ng generate component data-display
Edit the component file (‘src/app/data-display/data-display.component.ts’) to fetch and display the data:
// data-display.component.ts import { Component, OnInit } from '@angular/core'; import { DataService } from '../data.service'; @Component({ selector: 'app-data-display', templateUrl: './data-display.component.html', styleUrls: ['./data-display.component.css'] }) export class DataDisplayComponent implements OnInit { data: any[]; constructor(private dataService: DataService) {} ngOnInit(): void { this.dataService.getData().subscribe(data => { this.data = data; }); } }
Step 5: Display Data in the Component Template
Edit the component template file (src/app/data-display/data-display.component.html) to display the fetched data:
{{ item.name }}
{{ item.description }}
Step 6: Integrate Component in App Module
Finally, integrate the newly created component in the app.module.ts file:
// app.module.ts import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { HttpClientModule } from '@angular/common/http'; import { DataDisplayComponent } from './data-display/data-display.component'; @NgModule({ declarations: [ DataDisplayComponent, // ... other components ], imports: [ BrowserModule, HttpClientModule, ], providers: [], bootstrap: [DataDisplayComponent] // Set the component you want to display }) export class AppModule { }
Step 7: Run the Application
Run your Angular application using the following command:
ng serve
Visit ‘http://localhost:4200/’ in your browser to see the data displayed from the JSON file.
That’s it! You’ve successfully displayed data from a JSON file in an Angular application.