Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular4: how do access local json?

In Angular2 you could have a folder /data/ and a json file there and you could access it at localhost:4200/data/something.json.

This is no longer possible in Angular4.

Any ideea how to get it to work?

like image 863
Dan Neciu Avatar asked Apr 07 '17 10:04

Dan Neciu


People also ask

How do I read local JSON data?

Chrome allows you to access local JSON or other data files if you launch it with the --allow-file-access-from-files flag. I checked this with the code above on version 34.0. 1847.131 m; it should work on other versions as well.

How can I view JSON files?

Users can follow the below steps to open JSON files in Chrome or Firefox browsers. Right-click on the JSON file. Choose open with option from the menu. From the drop-down menu either choose Chrome or Firefox.

How do I view a JSON file in my browser?

Steps to open JSON files on Web browser (Chrome, Mozilla)Open the Web store on your web browser using the apps option menu or directly using this link. Here, type JSON View in search bar under the Extensions category. You will get the various extensions similar to JSON View to open the JSON format files.


1 Answers

you can use this code

@Injectable()
export class AppServices{

    constructor(private http: Http) {
         var obj;
         this.getJSON().subscribe(data => obj=data, error => console.log(error));
    }

    public getJSON(): Observable<any> {
         return this.http.get("./file.json")
                         .map((res:any) => res.json())
                         .catch((error:any) => console.log(error));

     }
}

here file.json is your local json file.

see here also

  • How to get a json file in angular2 using the Http class

also see the changlog of angular-cli for path

  • https://github.com/angular/angular-cli/blob/master/CHANGELOG.md#100-rc4-2017-03-20
like image 70
Pardeep Jain Avatar answered Sep 30 '22 14:09

Pardeep Jain