[Angular] how can I access to a local JSON file

3,145 views
Skip to first unread message

Bakadiri Abdelouahab

unread,
Jan 10, 2019, 1:50:14 PM1/10/19
to Angular and AngularJS discussion
Hello;
I have a local JSON file that conient a list of products, I want to access to it using a HTTP service through a get function, my class service and the json file are in the same folder, when i put:
getProducts(): Observable<Iproduct[]> {
return this._http.get<Iproduct[]>('./products.json');
}
I have a error like this:
Do you have any idea about where angular put this file while it build my application.
thank you.

Tito

unread,
Jan 10, 2019, 8:38:36 PM1/10/19
to Angular and AngularJS discussion
I saw this on SO

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

   constructor(private http: HttpClient) {
        this.getJSON().subscribe(data => {
            console.log(data)
        });
    }

    public getJSON(): Observable<any> {
        return this.http.get("./assets/mydata.json")
    }

Bakadiri Abdelouahab

unread,
Jan 11, 2019, 1:37:42 PM1/11/19
to ang...@googlegroups.com
So I must put my file in a asset folder. It’s work now thank you so much.

--
You received this message because you are subscribed to the Google Groups "Angular and AngularJS discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.
--
Envoyer depuis mon Iphone

Tito

unread,
Jan 11, 2019, 5:14:03 PM1/11/19
to Angular and AngularJS discussion
question for you, why not have this data in a backend like a database, mongodb etc? then let us say another 
instance of your same website or yet another totally different website needs to read the data, wouldnt it be better
to have this reside on a database that can be accessed by both websites via a service?

Tito

unread,
Jan 11, 2019, 5:15:28 PM1/11/19
to Angular and AngularJS discussion
ce que veux te dire c'est que si demain tu voudrais utilizer le data dans ce json d'un autre site, cette architecture est trop frgile :)

Bakadiri Abdelouahab

unread,
Jan 12, 2019, 6:46:43 AM1/12/19
to ang...@googlegroups.com
Salut. Merci. Je poursuis juste une formation en autoformation et j’utilise cette archetcture pour apprende autres fonctionnalités d’Angular. Merci bcp.
Reply all
Reply to author
Forward
0 new messages