Ionic Framework Cookbook

by Hoc Phan

Ionic Framework Cookbook Over 35 exciting recipes to spice up your application development with IonicAbout This Book Learn how to utilize the robust features of Ionic CLI and its framework to create develop and build your mobile app Explore new integrations with various Backend as a Services along with AngularJS modules for creative solutions Use out of the box Ionic functionalities customize existing components and add new components with this comprehensive step by step guideWho This Book Is ForIf you are a f

Publisher : Packt Publishing

Author : Hoc Phan

ISBN : 9781785287978

Year : 2015

Language: en

File Size : 14.13 MB

Category : Computers Technology

Ionic Cookbook

Over 20 exciting recipes to spice up your application
development with Ionic

Hoc Phan


Ionic Cookbook
Copyright © 2015 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system,
or transmitted in any form or by any means, without the prior written permission of the
publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the
information presented. However, the information contained in this book is sold without
warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers
and distributors will be held liable for any damages caused or alleged to be caused directly
or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies
and products mentioned in this book by the appropriate use of capitals. However, Packt
Publishing cannot guarantee the accuracy of this information.

First published: October 2015

Production reference: 1261015

Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78528-797-8


Project Coordinator

Hoc Phan

Judie Jose



Mike Hartington

Safis Editing

Chady Kassouf

Ted Morin

Hemangini Bari

Siva Prakash
Syed Iqrar Raza Zaidi

Abhinash Sahu

Acquisition Editors
Aaron Lazar

Production Coordinator

Rebecca Youé

Komal Ramchandani

Content Development Editor
Ritika Singh


Cover Work
Komal Ramchandani

Technical Editor
Edwin Moses
Copy Editors
Vedangi Narvekar
Jonathan Todd

This book is the result of 7 months of focused writing and coding by Hoc Phan, a talented
developer and dedicated writer with whom the Ionic team has collaborated on a past book.
Hoc is a dedicated Ionic community member who often speaks about Ionic, and we have the
utmost respect for his commitment to teaching others how to use Ionic in really effective
ways. This book provides recipes for getting a stew goin' with Ionic!
Hoc teaches developers how to set up a project by installing the Ionic command-line tool
(CLI) and creating a new project. He addresses platform-specific styles and how to target each
platform with SCSS and JavaScript. He explains how to integrate an Ionic app with Firebase
to create a connection to a real-time database. The book covers routing and navigation, as
well as running your app on a device (how to target the iOS and Android simulators).
For more advanced developers, the book explains how to integrate device APIs with ngCordova
by working with a device's camera. Hoc also addresses the topic of setting up the Whitelist
Plugin and working with CSP to make sure your app is secure.
Before I joined Ionic as a core team member and developer advocate, I was a developer and
created many internal hybrid apps for my company. Before I began building apps, I evaluated
multiple frameworks. Only Ionic provided a complete solution for hybrid mobile development.
I could focus on development, and Ionic handled architecture and design.
Ionic offers a complete ecosystem for building performant, beautiful mobile apps using one
code base, which saves time and money. We released the stable version of Ionic in May
2015 and have released alpha versions of four of our platform services since then. We
plan to continue to support the open source Ionic SDK and release new features, tools, and
services. As Ionic's developer advocate, I travel around the US to talk about Ionic and meet
Ionic developers, and my favorite part of my job is people's enthusiasm for Ionic. The Ionic
community is what makes Ionic great.
You'll find this book to be a great way to get cooking with Ionic and deep dive into the
SDK. We're glad to have you as a member of the Ionic community.
Mike Hartington
Developer Advocate,

About the Author
Hoc Phan is a technologist with a lot of experience in frontend development. He started

programming at the age of 12 with Pascal and Assembly on a 486 computer. The way he
learned was to start practicing right away even before figuring out concepts. Hoc worked
on many JavaScript projects in the past by teaching himself the framework using various
online sources. He was one of the first few developers who tested Ionic for its feasibility
as a JavaScript replacement of the native language of a device. He wrote the Full Stack
Mobile App with Ionic Framework book (for more information, visit
com/Full-Stack-Mobile-Ionic-Framework-ebook/dp/B00QF1H380/), which was
very well-received.
He has worked for both start-ups and large companies. By frequently speaking at local
meet-ups as well as cloud computing / Big Data industry events and conferences,
he learns from other experts. He holds an MBA degree from the University of Washington's
Michael G. Foster School of Business.

About the Reviewers
Mike Hartington is a JavaScript developer who has focused on hybrid technologies for
most of his career. By working on Ionic, he has been able to take his love of hybrid apps
to the next level, focussing on making a power SDK for developers of all skill levels. After
working with Arvind Ravulavaru on the Learning Ionic book by Packt Publishing, he used
his knowledge as an Ionic core team member to provide valuable feedback on this book's
content. He is thankful to the Ionic community for its support.

Chady Kassouf is an independent iOS and web development expert. He started

programming 23 years ago, and he hasn't stopped ever since. Seven years ago, he decided
to leave his job as a team leader in one of the leading digital agencies and to start his own
His interests apart from computers include arts, music, and fitness. He can be found online

Ted Morin is a software developer with a focus on frontend technologies. He enjoys JavaScript
in its many forms and is always exploring new frameworks and tools. He is currently juggling
different projects and jobs and pursuing an undergraduate degree in software engineering at
the University of Ottawa.

Siva Prakash has been working in the field of software development for the last 7

years. He is currently working for CISCO, Bangalore. He has an extensive experience in the
development of desktop, mobile, and web-based applications in ERP, telecom, and the digital
media industry. He has a passion for learning new technologies and sharing knowledge thus
gained with others. He has worked on Big Data technologies for the digital media industry.
He loves trekking, travelling, listening to music, reading books, and blogging.

Syed Iqrar Raza Zaidi is a software engineer with an experience of 4 years. He is

currently designated as a Technical Lead at Bond Internet Consultancy LLC in Dubai, UAE.
He is an organized, energetic, and dedicated software developer at work who is motivated
to be of service by positively contributing towards every piece of work. He has vast knowledge
of core JavaScript, and he has dabbled in PHP and Node.js. He built multiple web portals,
web applications, mobile hybrid applications, and 2D and 3D games that delight and inform
users using the latest web technologies.
He received the Employee Performance Award at Systems Limited in December 2014.
He is a Microsoft Community Contributor as well as a technology leader at Mohammad
Ali Jinnah University, Karachi, Pakistan.
You can view his LinkedIn profile by visiting
raza-zaidi/6a/201/a62. If you're interested in his work, you can drop him an email
([email protected]).
Whatever I am today is because of my parents' prayers and my family's love.
Support files, eBooks, discount offers, and more
For support files and downloads related to your book, please visit
Did you know that Packt offers eBook versions of every book published, with PDF and ePub
files available? You can upgrade to the eBook version at and as a print
book customer, you are entitled to a discount on the eBook copy. Get in touch with us at
[email protected] for more details.
At, you can also read a collection of free technical articles, sign up for
a range of free newsletters and receive exclusive discounts and offers on Packt books and

Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book
library. Here, you can search, access, and read Packt's entire library of books.

Why Subscribe?

Fully searchable across every book published by Packt


Copy and paste, print, and bookmark content


On demand and accessible via a web browser

Free Access for Packt account holders
If you have an account with Packt at, you can use this to access
PacktLib today and view 9 entirely free books. Simply use your login credentials for
immediate access.

Table of Contents
Preface iii
Chapter 1: Creating Our First App with Ionic

Introduction 1
Setting up a development environment
Creating a HelloWorld app via CLI
Creating a HelloWorld app via Ionic Creator
Copying examples from Ionic Codepen Demos
Viewing the app using your web browser
Viewing the app using iOS Simulator
Viewing the app using Xcode for iOS
Viewing the app using Genymotion for Android
Viewing the app using Ionic View
Customizing the app folder structure

Chapter 2: Managing States and Navigation


Chapter 3: Adding Device Features Support


Introduction 33
Creating a tab interface with nested views
Creating a multistep form with validation

Introduction 57
Taking a photo using the device camera
Capturing video and allowing playback
Composing an email with an attachment from an app
Picking and adding a contact
Adding Google Maps with geocoding support


Table of Contents

Chapter 4: Offline Data Storage

Creating a to-do app using ngStorage for Local Storage
Creating a social networking app using SQLite



Chapter 5: Handling Gestures and Events


Chapter 6: App Theme Customization


Chapter 7: Extending Ionic with Your Own Components


Chapter 8: User Registration and Authentication


Chapter 9: Saving and Loading Data Using Firebase


Chapter 10: Finalizing Your Apps for Different Platforms




Detecting drag events with a gesture coordinate
Communication between a view, controller, and directive using events


Introduction 135
Customizing themes for specific platforms
Creating an introduction screen with a custom header
Creating a scroll progress bar directive
Creating a custom filter
Animating an app using requestAnimationFrame with event binding


Introduction 170
Configuring a Facebook app with the Firebase authentication
Configuring a Twitter app with the Firebase authentication
Configuring a Google+ project with the Firebase authentication
Creating an Ionic social authentication project for Facebook using
Creating a LinkedIn app and configuring the authentication in Auth0
Integrating Auth0's LinkedIn authentication in an Ionic project
Saving array data to Firebase
Rendering a large Firebase data set using collection-repeat
Saving form data to Firebase


Introduction 227
Building and publishing an app for iOS
Building and publishing an app for Android
Using PhoneGap Build for cross-platform applications


The world of mobile development is fragmented, with many platforms, frameworks, and
technologies. Ionic is intended to fill this gap with its open source HTML5 mobile app framework
that lets developers build native-feeling apps using web technologies such as HTML, CSS, and
AngularJS. Ionic makes it easy for frontend developers who want to become app developers.
The framework provides superior performance with deep Cordova integration and a
comprehensive set of tools for prototyping, backend support, and deployment.
This book will take you through the process of developing a cross-platform mobile app using
just HTML5 and JavaScript based on Ionic. You will start first by getting familiarized with the
CLI and learning how to build and run an app. You will have a look at some common features
of real-world mobile apps such as authenticating a user and receiving and saving data using
either Firebase or Local Storage.
Next, the book will explain how Ionic integrates with Cordova to support native device features
by using ngCordova and takes advantage of the existing modules around its ecosystem. You
will also explore the advanced topics related to extending Ionic to create new components.
Finally, the book will show you how to customize the Ionic theme and build the app for all

What this book covers
Chapter 1, Creating Our First App with Ionic, introduces the Ionic framework and provides
instructions for setting up the development environment and quickly creating and running
the first app.
Chapter 2, Managing States and Navigation, walks through some examples of how to
manage views, states, and the overall navigation within the app. This can be done via
either the UI-Router component of AngularJS or the out-of-the-box Ionic directives.
Chapter 3, Adding Device Features Support, explains how to use ngCordova to access native
device functionalities such as the camera (photo and video), the contact list, e-mail, and map.

Chapter 4, Offline Data Storage, explains how to work with persistent data when the device
is offline. You will understand the advantages and disadvantages of using Local Storage
versus SQLite.
Chapter 5, Handling Gestures and Events, explains how a touch event works and how to
process these events to create a better interaction or a custom component.
Chapter 6, App Theme Customization, provides instructions on how to customize an app for
different platforms and create an introduction screen for your own branding.
Chapter 7, Extending Ionic with Your Own Components, takes a deep dive into the AngularJS
directive and filter customization. You will learn how to leverage events from the core Ionic
components and use requestAnimationFrame for an improved animation performance.
Chapter 8, User Registration and Authentication, explains the different methods that can be
used to authenticate a user and how the Firebase authentication system works.
Chapter 9, Saving and Loading Data Using Firebase, walks through some examples,
such as those related to storing and retrieving data, using Firebase as the backend.
Also, you will learn how to manage and render large datasets.
Chapter 10, Finalizing Your Apps for Different Platforms, provides instructions for
performing the final steps of getting an app published.

What you need for this book
You need the following to work with the examples in this book:

A Mac computer with Mac OS X Yosemite and root privilege


Or a PC with Windows 7 or later with Administrator privileges


iPhone 5 or later


An Android device with Android 5.x or later (optional)


A Windows phone device (optional)

Who this book is for
Ionic Cookbook is intended for frontend developers who want to take advantage of their
existing skills to develop cross-platform mobile apps. This book will help you become an
intermediate or advanced Ionic developer by covering in-depth topics about AngularJS,
Cordova, and Sass. Since Ionic is open source, there is a large community that supports
this framework for you to continue the learning journey.


© 2018-2019 All rights reserved