Skip to main content

Reading a JSON File With Ionic Framework & Angular Js

Introduction

AngularJS is perfect for Single Page Applications (SPAs).
AngularJS extends HTML attributes with Directives, and binds data to HTML with Expressions.
$http is an AngularJS service for reading data from remote servers.

1.Create the project

ionic start Test blank

2. JSON File to read name as Customers_JSON.json

[ { "Name" : "Santosh Shinde", "City" : "Abc_Sangamner", "Country" : "India" }, { "Name" : "Bhausaheb Sangale", "City" : "Sinnar", "Country" : "India" }, { "Name" : "Derik Taylor", "City" : "Indianapolis", "Country" : "USA" } ]

3.Write controller to get JSON data controllerJs.js

var app = angular.module('myApp', ['ionic']); app.controller('customersController', ['$scope', '$http', function($scope,$http) { $http.get("http://myurl/json/Customers_JSON.json") .success(function (response) { $scope.names = response; }); }]);

The AngularJS application is defined by ng-app. The ng-controller directive names the controller object.The customersController function is a standard JavaScript object constructor.AngularJS will invoke customersController with a $scope and $http object.
$scope is the application object (the owner of application variables and functions).$http is an XMLHttpRequest object for requesting external data.$http.get() reads static JSON data from http://myurl/json/Customers_JSON.json.
If success, the controller creates a property (names) in the scope, with JSON data from the server.
4.To show json data on list index.html

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width"> <title></title> <link href="lib/ionic/css/ionic.css" rel="stylesheet"> <link href="css/style.css" rel="stylesheet"> <!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above <link href="css/ionic.app.css" rel="stylesheet"> --> <!-- ionic/angularjs js --> <script src="lib/ionic/js/ionic.bundle.js"></script> <!-- cordova script (this will be a 404 during development) --> <script src="cordova.js"></script> <!-- your app's js --> <script src="js/controllerJs.js"></script> </head> <body ng-app="myApp" ng-controller="customersController"> <ion-pane> <ion-header-bar class="bar-stable"> <h1 class="title">Ionic Blank Starter</h1> </ion-header-bar> <ion-content> <ion-list> <ion-item ng-repeat="x in names|orderBy:'City'"> {{ x.Name + ', ' + x.Country+', '+x.City}} </ion-item> </ion-list> </ion-content> </ion-pane> </body> </html>

5.Build the Project

ionic build android


Snapshot Output :

Comments

  1. Thank you for sharing this knowledge in a blogpost.Really simple and even more effective and this worked great, very useful tips
    Angularjs Training In Hyderabad

    ReplyDelete
  2. Thanks you help me, you have github for see this example?

    ReplyDelete

Post a Comment

Popular posts from this blog

File Upload & Download With ng-cordova File Transfer Plugin In Ionic Framework

Using the AngularJS extension set , ngCordova , with Ionic Framework and the Apache Cordova File Transfer plugin, you can easily upload files to a remote server and download files from a remote server. 1.Create the project ionic start Test blank cd Test ionic platform add android 2.Add Plugin org.apache.cordova.file-transfer https://github.com/apache/cordova-plugin-file-transfer This plugin allows you to upload and download files. This plugin defines global FileTransfer, FileUploadOptions Constructors. Although in the global scope, they are not available until after the deviceready event . Installation cordova plugin add cordova plugin add cordova-plugin-file-transfer     2.    org.apache.cordova.file             https://github.com/apache/cordova-plugin-file             This plugin implements a Fi...

What exactly means MVW design pattern ?

What is a MVW framework? The abbreviation stands for 'Model - View - Whatever'.  Well there are many different JavaScript frameworks available , all invented for the same purpose. They all try to separate the presentation logic from the business logic where JavaScript holds the model and logic, and html the presentation layer. Quick overview : You can change the model without changing the view and vice-versa Unit testing is easy These are the core benefits of using such a framework. By separating presentation and business logic changes to the code are easier to implement and take less time. Another benefit is that code can be reused from a controller thus saving more time. Angularjs makes the code also shorter in comparison to other frameworks, which improves code stability. Less code means minor potential for bugs. For several years +AngularJS was closer to MVC (or rather one of its client-side variants), but over time and thanks to many refactorings...

WooCommerce REST API with Ionic Framework

Step 1 : Enabling REST API To enable the REST API within WooCommerce, visit the WooCommerce > Settings > API tab and tick the Enable REST API checkbox. Step 2 : Generating API keys The WooCommerce REST API works on a key system to control access. These keys are linked to WordPress users on your website. To create or manage keys for a specific WordPress user, go to WooCommerce > Settings > API > Keys/Apps. To get started, select Add Key. Select the User you would like to generate a key for in the User field and add a Description. Choose the level of access for this API key, which can be Read access, Write access or Read/Write access. Then select the Generate API Key button and WooCommerce will generate API keys for that user. Now that keys have been generated, you should see two new keys, a QRCode, and a Revoke API Key button. These two keys are your Consumer Key and Consumer Secret. Place your consu...