Getting Started with Ionic

Andrew Chalkley

June 8, 2015

-

7 min read

Learn

What is Ionic?

Ionic is a set of performance-focused, beautifully designed HTML, CSS and JavaScript components optimized for building mobile applications.

Ionic is composed of Cordova, the open sourced part of the PhoneGap platform, Sass and AngularJS.

It allows you to create cross-platform applications meaning you can write your code once and deploy it anywhere; iOS, Android and Windows.

Check out my other post on why Ionic is challenging your assumptions on the Native vs Hybrid debate.

What We’re Building

We’re going to go out to the web and pull in the badges on my Treehouse profile and list them like this:

Only that the scrolling is a lot more smooth than this gif!

Prerequisites

Windows

It’s recommended if you’re on Windows for you to use Visual Studio 2015 Community RC. Since Visual Studio 2015 Community Edition is still in Release Candidate phase, your mileage may vary on installing the Ionic Tabs Template to create your project.

For the rest of the tutorial I’m assuming you’re on Mac or Linux. If you get the Ionic Tabs Template installed successfully, you should be able to follow along.

Mac and Linux

You need to have Node.js and NPM installed. Check out the following posts for help if you haven’t done so already.

Installing Ionic Command Line Tools

Still on Mac and Linux, open up your terminal and type:

npm install -g cordova ionic

Creating the Project

To create your project you use the following command:

ionic start <project name> <template>

I’m going to call my project treehouse_badges and I’m going to start with the tabs example project.

ionic start treehouse_badges tabs

You’ll see an output like this with some helpful commands:

Your Ionic project is ready to go! Some quick tips:

 * cd into your project: $ cd treehouse_badges

 * Setup this project to use Sass: ionic setup sass

 * Develop in the browser with live reload: ionic serve

 * Add a platform (ios or Android): ionic platform add ios [android]
   Note: iOS development requires OS X currently
   See the Android Platform Guide for full Android installation instructions:
   https://cordova.apache.org/docs/en/edge/guide_platforms_android_index.md.html

 * Build your app: ionic build <PLATFORM>

 * Simulate your app: ionic emulate <PLATFORM>

 * Run your app on a device: ionic run <PLATFORM>

 * Package an app using Ionic package service: ionic package <MODE> <PLATFORM>

For more help use ionic --help or ionic docs

Visit the Ionic docs: http://ionicframework.com/docs

Then it asks if you want to sign up for some services like Push Notifications or Ionic View, which is a way to share your app with others without going through the Apple or Google App Stores.

Create an ionic.io account to send Push Notifications and use the Ionic View app?
(Y/n): 

I’m going to say no for now. And get on with our project.

The Project

Change directory into your Ionic project.

cd treehouse_badges 

And there’s already a basic scaffold for your app. We are mainly concerned with the www/js and www/templates folders.

First, let’s start the project and preview it in a browser by using the ionic serve command in the console. You may be prompted an address to serve your app to.

Multiple addresses available.
Please select which address to use by entering its number from the list below:
 1) 192.0.1.100 (en0)
 2) localhost
Address Selection:  

If you pick localhost your app in development will be available to view from your own machine, however other people on your local network will not be able to see it (which may or may not be a good idea). If you want to share it with other’s on your network pick the IP address (xx.xx.xx.xx). You won’t be prompted again by Ionic.

Once you’ve selected something a web browser with the tabs, Ionic app gets launched. It also uses live reload, so as you change the files it will update in the browser.

Dashboard

Let’s modify the dashboard (templates/tab-dash.html) to be our list view with all of our badges. Here’s what the original contents looks like…

<ion-view view-title="Dashboard">
<ion-content class="padding">
<div class="list card">
<div class="item item-divider">Recent Updates</div>
<div class="item item-body">
<div>
There is a fire in <b>sector 3</b>
</div>
</div>
</div>
<div class="list card">
<div class="item item-divider">Health</div>
<div class="item item-body">
<div>
You ate an apple today!
</div>
</div>
</div>
<div class="list card">
<div class="item item-divider">Upcoming</div>
<div class="item item-body">
<div>
You have <b>29</b> meetings on your calendar tomorrow.
</div>
</div>
</div>
</ion-content>
</ion-view>
view raw tab-dash.html hosted with ❤ by GitHub

Let’s set the view-title attribute on the ion-view to "Badges". And let’s get rid of all the content inside the ion-content.

<ion-view view-title="Badges">
<ion-content class="padding">
</ion-content>
</ion-view>
view raw tab-dash.html hosted with ❤ by GitHub

It should look a bit like this:

Badges step 1

But how are we going to get the badge data? If you go to anyone’s Treehouse profile and add .json to the end of the profile URL you get a JSON representation of the badges the student has taken with a breakdown of points based on topic area. Here’s a sample of mine at http://teamtreehouse.com/chalkers.json.

{
"name": "Andrew Chalkley",
"profile_name": "chalkers",
"profile_url": "http://teamtreehouse.com/chalkers",
"gravatar_url": "https://uploads.teamtreehouse.com/production/profile-photos/26170/thumb_TeacherShoot-Andrew.jpg",
"gravatar_hash": "7139178cce7d4441b3ea43af22600e8b",
"badges": [
{
"id": 49,
"name": "Newbie",
"url": "http://teamtreehouse.com/chalkers",
"icon_url": "https://achievement-images.teamtreehouse.com/Generic_Newbie.png",
"earned_date": "2012-07-23T19:59:34.000Z",
"courses": [
]
},
{
"id": 26,
"name": "Introduction",
"url": "http://teamtreehouse.com/library/html/introduction",
"icon_url": "https://achievement-images.teamtreehouse.com/HTML_Basics.png",
"earned_date": "2012-07-23T21:57:24.000Z",
"courses": [
{
"title": "HTML",
"url": "http://teamtreehouse.com/library/html",
"badge_count": 1
},
{
"title": "Introduction",
"url": "http://teamtreehouse.com/library/html/introduction",
"badge_count": 1
}
]
},
...
],
"points": {
"total": 14014,
"HTML": 1842,
"CSS": 1136,
"Design": 415,
"JavaScript": 3353,
"Ruby": 1528,
"PHP": 668,
"WordPress": 19,
"iOS": 710,
"Android": 72,
"Development Tools": 1085,
"Business": 146,
"Python": 199,
"Java": 377,
"Digital Literacy": 330
}
}
view raw chalkers.json hosted with ❤ by GitHub

Let’s open js/controllers.js, in here there are all the Angular controllers for the tab views.

angular.module('starter.controllers', [])
.controller('DashCtrl', function($scope) {})
.controller('ChatsCtrl', function($scope, Chats) {
// With the new view caching in Ionic, Controllers are only called
// when they are recreated or on app start, instead of every page change.
// To listen for when this page is active (for example, to refresh data),
// listen for the $ionicView.enter event:
//
//$scope.$on('$ionicView.enter', function(e) {
//});
$scope.chats = Chats.all();
$scope.remove = function(chat) {
Chats.remove(chat);
}
})
.controller('ChatDetailCtrl', function($scope, $stateParams, Chats) {
$scope.chat = Chats.get($stateParams.chatId);
})
.controller('AccountCtrl', function($scope) {
$scope.settings = {
enableFriends: true
};
});
view raw controllers.js hosted with ❤ by GitHub

There’s no code in the DashCtrl or the dashboard controller. Let’s use AngularJS’ $http service to get the JSON data. We’ll inject the $http dependency, call the get method and pass in my profile URL, "http://teamtreehouse.com/chalkers.json". On a successful retrieval of information, we want to add the badges array into the $scope. This makes sure the variable badges will be available in our views. While we’re at it let’s remove all the other controllers.

angular.module('starter.controllers', [])
.controller('DashCtrl', function($scope, $http) {
//Get profile information
$http.get('https://teamtreehouse.com/chalkers.json').
success(function(data, status, headers, config) {
// this callback will be called asynchronously
// when the response is available
$scope.badges = data.badges;
}).
error(function(data, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
});
view raw controllers.js hosted with ❤ by GitHub

Let’s quickly update the templates/tabs.html file to only include one tab, from this:

<!--
Create tabs with an icon and label, using the tabs-positive style.
Each tab's child <ion-nav-view> directive will have its own
navigation history that also transitions its views in and out.
-->
<ion-tabs class="tabs-icon-top tabs-color-active-positive">
<!-- Dashboard Tab -->
<ion-tab title="Status" icon-off="ion-ios-pulse" icon-on="ion-ios-pulse-strong" href="#/tab/dash">
<ion-nav-view name="tab-dash"></ion-nav-view>
</ion-tab>
<!-- Chats Tab -->
<ion-tab title="Chats" icon-off="ion-ios-chatboxes-outline" icon-on="ion-ios-chatboxes" href="#/tab/chats">
<ion-nav-view name="tab-chats"></ion-nav-view>
</ion-tab>
<!-- Account Tab -->
<ion-tab title="Account" icon-off="ion-ios-gear-outline" icon-on="ion-ios-gear" href="#/tab/account">
<ion-nav-view name="tab-account"></ion-nav-view>
</ion-tab>
</ion-tabs>
view raw tabs.html hosted with ❤ by GitHub

To this:

<!--
Create tabs with an icon and label, using the tabs-positive style.
Each tab's child <ion-nav-view> directive will have its own
navigation history that also transitions its views in and out.
-->
<ion-tabs class="tabs-icon-top tabs-color-active-positive">
<!-- Dashboard Tab -->
<ion-tab title="Status" icon-off="ion-ios-pulse" icon-on="ion-ios-pulse-strong" href="#/tab/dash">
<ion-nav-view name="tab-dash"></ion-nav-view>
</ion-tab>
</ion-tabs>
view raw tabs.html hosted with ❤ by GitHub

We can alter the title attribute to "Badges". And how about the icon? It says ion-ios-pulse. Let’s head over to Ionic documentation and follow the Ionicons link.
You can type in a word into the search box, I typed in award to look for appropriate icons.

Click on an icon to reveal its name. I like the look of ion-ribbon-b so let’s update both the icon-off and icon-on attributes to equal ion-ribbon-b.

<!--
Create tabs with an icon and label, using the tabs-positive style.
Each tab's child <ion-nav-view> directive will have its own
navigation history that also transitions its views in and out.
-->
<ion-tabs class="tabs-icon-top tabs-color-active-positive">
<!-- Dashboard Tab -->
<ion-tab title="Badges" icon-off="ion-ribbon-b" icon-on="ion-ribbon-b" href="#/tab/dash">
<ion-nav-view name="tab-dash"></ion-nav-view>
</ion-tab>
</ion-tabs>
view raw tabs.html hosted with ❤ by GitHub

You should have something like this:

Now for displaying the badges! Ionic has a number of Angular Directives or view components for you to use. To see what’s available head over to the Docs and go to JavaScript. On the left-hand side, you should see Lists.

Copy the basic usage over to your tab-dash.html file and replace items with badgesand item with badge. Replace the contents with {{badge.name}} like so:

<ion-view view-title="Badges">
<ion-content class="padding">
<ion-list>
<ion-item ng-repeat="badge in badges">
{{badge.name}}
</ion-item>
</ion-list>
</ion-content>
</ion-view>
view raw tab-dash.html hosted with ❤ by GitHub

We’re getting close!

Let’s add images. We can include an image tag with the ng-src attribute of our badge‘s icon_url.

<ion-view view-title="Badges">
<ion-content class="padding">
<ion-list>
<ion-item ng-repeat="badge in badges">
<img ng-src="{{badge.icon_url}}">{{badge.name}}
</ion-item>
</ion-list>
</ion-content>
</ion-view>
view raw tab-dash.html hosted with ❤ by GitHub

But when you look at it in the browser, it looks…a bit off.

Ionic provides CSS to help style components to look more “natural”. Once again, jump into the Docs, CSS this time, and then Lists. There’s an example for thumbnails. You can include the class of item-thumbnail-left or -right. I like the thumbnail on the left.

<ion-view view-title="Badges">
<ion-content class="padding">
<ion-list>
<ion-item class="item-thumbnail-left" ng-repeat="badge in badges">
<img ng-src="{{badge.icon_url}}">{{badge.name}}
</ion-item>
</ion-list>
</ion-content>
</ion-view>
view raw tabs-dash.html hosted with ❤ by GitHub

And there you have it!

You’ve created your first Ionic app getting live data from the internet.

Next Steps

You can take this project further by:

  1. adding an error handler to the $http request
  2. add the earned_date to the list-item
  3. add a loading message before the web request and hide it once it’s completed.
  4. remove all references to “Dash”/”Dashboard”
  5. use the Ionic command line interface (CLI) to add iOS or Android platform to your project and deploy it to a simulator

You can take a look at my solution here.

Whilst we don’t have any Ionic Courses or Workshops yet on Treehouse, we have courses on a lot of Ionic’s dependencies, such as HTML, CSS, JavaScript, AngularJS, Sass, Node.js and much more!

10 Responses to “Getting Started with Ionic”

  1. Very interesting post! Thx a lot 🙂

  2. Rahul Joshi on May 9, 2016 at 3:26 am said:

    I am trying to install Ionic on Windows 7 machine but getting this error:

    gyp verb check python checking for Python executable “python2” in the PATH
    gyp verb `which` failed Error: not found: python2

  3. Excellent Post.Nice Explanation.Thanks for the share!.

  4. Jayesh Jodhawat on February 10, 2016 at 1:10 pm said:

    Fantastic Tutorial.
    Thank You 🙂

  5. Amazing tutorial, thank you.

  6. Samuel on June 15, 2015 at 5:57 am said:

    Really cool!
    Are you planning to do any Ionic courses?

  7. Milos Nikolic on June 12, 2015 at 6:15 am said:

    Very interesting. It is also interesting that you go to http://ionicframework.com/getting-started/ and click More and then realize that dropdown is not correctly displayed. A bit unprofessional?

  8. Nice post! Ionic is awesome, but I don’t like Angular, and thats why I’m using Meteoric http://meteoric.github.io to use the best of Meteor + Ionic in my app http://99folks.com 🙂

  9. This was much easier to understand than I thought it would be. My friend just learned ionic and said it took him about a month and still doesn’t really get it. I feel like I figured it out just reading this in a couple minutes. Thanks for the share!

Leave a Reply

You must be logged in to post a comment.

You might also like other posts...

Learn to code with Treehouse

Start your 7 day free trial today and get access to hundreds of video courses in web development, design and business!

Learn more