How to fetch JSON file in Angular 2

Discussão em 'Angular' iniciado por Vishu, Outubro 23, 2024.

  1. Vishu

    Vishu Guest

    as I am new to the Angular, can anyone please give a simple solution on loading the JSON file data using angular 2.

    My code is like below

    Index.html


    <html>
    <head>
    <title>Angular 2 QuickStart</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="css/styles.css">
    <!-- 1. Load libraries -->
    <!-- Polyfill(s) for older browsers -->
    <script src="node_modules/core-js/client/shim.min.js"></script>
    <script src="node_modules/zone.js/dist/zone.js"></script>
    <script src="node_modules/reflect-metadata/Reflect.js"></script>
    <script src="node_modules/systemjs/dist/system.src.js"></script>
    <!-- 2. Configure SystemJS -->
    <script src="systemjs.config.js"></script>
    <script>
    System.import('app').catch(function(err){ console.error(err); });
    </script>
    </head>
    <!-- 3. Display the application -->
    <body>
    <my-app>Loading...</my-app>
    </body>
    </html>

    app.component.ts


    import {Component} from '@angular/core';

    @Component({
    selector: 'my-app',
    template: `
    <div id="main">
    Main Div
    <div id = "header"></div>
    <div id = "content">
    <ul class="games">
    <li>

    </li>
    </ul>
    </div>
    </div>
    `
    })
    export class AppComponent {
    }

    games.json

    {
    "games":[
    {
    "title":"Primary Operations",
    "enabled":true
    },
    {
    "title":"Curated Games",
    "enabled":false
    }
    ]
    }

    I want to fetch all games from games.json into li at app.component.ts Please advise in detail.

    Continue reading...

Compartilhe esta Página