Monday, September 7, 2015

JSON with Angular

Hi, I have a simple JSON file with name "GameInfo.json", as below.
JSON
[
    {
        "GameName": "Game 1",
        "Client": "Self",
        "Lines": 5,
        "Feature": "",
        "LineType": "Lines",
        "GameImage": "game1.jpg"
    },
    {
        "GameName": "Game 2",
        "Client": "Self",
        "Lines": 5,
        "Feature": "No Feature",
        "LineType": "Lines",
        "GameImage": "no-preview.jpg"
    }
]

Now Load this file as below through json
JS
var app = angular.module('GameInformation', []);
app.controller('gameInfo', function($scope, $http)
{
$http.get("GameInfo.json").success(function(response){
$scope.gameDetails = response;
}).error(function(data, status, headers, config) {
                console.log('error loading json');
        });
});

Now to show this, follow the below step in html page

1. Create a table to show these details, then iterate the result for this.

        <div class="Title">GAME INFORMATION</div>
<div class="Heading">
<div class="Cell1">S No.</div>
<div class="Cell1">Game Name</div>
<div class="Cell1">Client</div>
<div class="Cell1">Lines</div>
<div class="Cell1">Feature</div>
<div class="Cell1">Line Type</div>
<div class="Cell1">Game Image</div>
</div>
<div id="rows{{$index}}" class="Row" ng-repeat="info in gameDetails">
<div class="CellCenter">{{$index+1}}</div>
<div class="Cell">{{info.GameName}}</div>
<div class="Cell2">{{info.Client}}</div>
<div class="Cell">{{info.Lines}}</div>
<div class="Cell">{{info.Feature}}</div>
<div class="Cell">{{info.Lines}}</div>
<div class="Cell">{{info.LineType}}</div>
<div class="Cell">{{info.GameImage}}</div>
</div>
Thats it :)

First Step in Angular

Hi,

Here are the steps to create a Angular App or Site

1. Download Angular or put updated link angular n your HTML, as below

<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular-animate.js"></script>

2. Now Place ng-app attribute into html or body or in div tag, as below

<body ng-app="GameInformation">

3. Now place ng-controller into div accordingly, as below

<div id="completeGames" ng-controller="gameInfo">

4. Now Create a JS file and write the below code based on mentioned ng-app and ng-controller name

var app = angular.module('GameInformation', []);
app.controller('gameInfo', function($scope, $http)
{
   console.log("App Initiated");
});