[Fixed] Best way to store/display data in angular

Issue

So I have a FAQ page which I have developed in angular and I have hardcoded all the data in a html file .I just wanted to know is there a better way of displaying the data like storing it in a XML/JSON/TXT file and read the file and display it or store the XML/JSON/TXT file in angular and read form it**(if storing it where to store it)**.

.Browsed for a few articles didn’t find anything helpful ,any guidance will be appreciated. I am new to angular so just looking for some advice ,thanks in advance.

FAQ-image

Solution

If your data is static I recommend storing it in a JSON file inside your angular application under the assets folder.

You can then use Angular HttpClient to retrieve the content of the file.

In this example, the file is called ‘data.json’ and is stored under the ‘assets’ folder:


import { HttpClient } from '@angular/common/http';

@Component({
 selector: 'my-component',
 templateUrl: './my-component.html',
 styleUrls: ['./my-component.css']
})

export class MyComponent implements OnInit {
 constructor(private http: HttpClient) { }
 ngOnInit() {
   this.http.get('/assets/data.json').subscribe().then(data => console.log(data));
 }
}

You can find more information here.

Leave a Reply

(*) Required, Your email will not be published