Reading JSON by AngularJS Service
January 07, 2015
In AngularJS we can access external data by using angular service. It is called a service or AngularJS service or $http service in AngularJS. An angularJS service code reads data in JSON format. as below.
<div ng-app="" ng-controller="addressController"> <ul> <li ng-repeat="x in addlist"> {{ x.Name + ',' + x.City + ',' + x.Country}} <br><br> </li> </ul> </div>
Example
In example we create two file one file is HTML file and another is json.js file.<!DOCTYPE html> <html> <head> <script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script> </head> <body> <div ng-app="" ng-controller="addressController"> <ul> <li ng-repeat="x in addlist"> {{ x.Name + ',' + x.City + ',' + x.Country}} <br><br> </li> </ul> </div> <script> function addressController($scope,$http) { $http.get("json.js") .success(function(response) { $scope.addlist = response; } ); } </script> </body> </html>
json.js
[ { "Name" : "Amit", "City" : "Mumbai", "Country" : "India" }, { "Name" : "Ram Singh", "City" : "Ghaziabad", "Country" : "India" }, { "Name" : "Ankit Pandey", "City" : "New Delhi", "Country" : "India" }, { "Name" : "Subhash", "City" : "Noida", "Country" : "India" }, { "Name" : "Jurfuddin", "City" : "Kerla", "Country" : "India" } ]
Output
