Search code examples
angularangular-services

Unable to fetch data placed in assets folder in angular 2


In my sample application I have created a simple json file institute.json which has data for instituteName and typ

I have created a service class called get-data.service.ts to fetch the data from json file placed in assets folder.

But when I am trying to access the URL http://localhost:4200/assets/institute.json , its redirecting me to the login page of my application.

I have created routeguard in my application for the admin user ,is it because of the routegaurd I am unable to access the URL http://localhost:4200/assets/institute.json???

Please access my sample app here

When I am trying to fetch data from the service in my admin.component.ts file , its giving me the following error.

GET http://localhost:4200/assets/institute.json 404 (Not Found)

Any help would be greatly appreciated .Please guide me what am I missing here...?


Solution

  • Your assets folder is not copied to your compiled code. Its as simple as changing your .angular-cli.json file from

    {
      "apps": [{
        "styles": ["styles.css"]
      }]
    }
    

    to

    {
      "apps": [{
        "styles": ["styles.css"],
        "assets": ["assets"]
      }]
    }
    

    I fixed it in this sample here.