When it comes to working in the tech freelance industry, it’s extremely important to always keep learning. It’s always beneficial to add more skills to your developer portfolio. This shows future clients that you are driven and that you like to stay on top of the latest technology trends. Moses from Silicon Valley recommends to, “always have side projects to help keep you constantly investigate and to try new things. You never know when you’ll have to use those skills.” Now, this app tutorial may help you get to the next step in your career so why not give it a shot?

Ionic is a popular mobile app framework that allows developers to write their apps using web technologies without having to have a deep understanding of mobile languages such as Java or Objective C. In this app tutorial, we will cover:

  • How to install Ionic
  • The configure for building on multiple platforms
  • How to preview and debug the app
  • How to build a “hello world” application

Step 1: Install Ionic

First, install Node.js. Then, update the latest npm and install the latest Cordova and Ionic.

Update the latest npm:

$ npm install npm - g

Then install the latest Cordova and Ionic using npm.

$ npm install - g ionic cordova

Let’s create and name your first app tutorial as ‘helloworld’ with a blank template.

$ ionic start helloworld blank

Step 2: Configuring Platforms

Cordova gives you the power to write computer programming code once and run on many mobile app platforms (iOS, Android, Blackberry, Windows, etc). But you have to explicitly tell Cordova (with Ionic) which mobile app platform should be included in any given project. For our HelloWorld app, we will be trying it out with two platforms (iOS and Android).

$ cd HelloWorld
$ ionic platform add ios
$ ionic platform add android

Step 3: Previewing the App

Firstly, we can use the server option to preview our app tutorial in the browser using Ionic.

$ ionic serve

Next, the command will open up a browser at http://localhost:8100. As a result of browser screen is too large to preview a mobile app but we can use the Responsive Design View of Browser in order to view it properly.

iOS & Android App Tutorial Using HTML5

Responsive Design View of Browser

$ ionic build Android
$ ionic emulate Android

Most importantly, be sure to substitute “android” with “iOS” to build for iOS.

Note:

  • Follow the Android and iOS mobile app platform guides to install required platform dependencies.
  • iOS development requires Mac OS X. iOS simulator through the Ionic CLI Since the Ionic CLI requires an ios-sim npm package, this can be installed with the command sudo npm -g install ios-sim.

Step 4: Making a helloworld app instead of a blank app

Ionic is a basically a SPA web app. We need a master index.html but we don’t need routes since the helloworld app is just a one view app.

In this app tutorial, it is important that we first update our app’s www/index.html and www/js/app.js to make sure the structure looks like the one below.

<!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">
    <link href="css/ionic.app.css" rel="stylesheet">
    <script src="lib/ionic/js/ionic.bundle.js"></script>
    <script src="cordova.js"></script>
    <script src="js/app.js"></script>
</head>

<body ng-app="helloworld">
    <ion-pane>
        <ion-header-bar class="bar-stable">
            <h1 class="title">Say hello to the world with Ionic</h1>
        </ion-header-bar>
        <ion-content>
            <ion-list>
                <ion-item>Hello world</ion-item>
                <ion-item>Bonjour le monde</ion-item>
            </ion-list>
        </ion-content>
    </ion-pane>
</body>

</html>

App.js

// Ionic Starter App 
angular.module('helloworld', ['ionic']);

As seen in the code above, using the ion-list, we now have hard coded 2 items in our to-do list. In order for the app to be fully functional, we should be able to add and view list items dynamically.

Next…

Inside, www/js/ create a controllers.js file and define a new controller called HelloController inside it. Here is how the controllers.js file looks:

angular.module('helloworld.controllers', [])
    .controller('HelloController', function($scope) {
        $scope.helloList = [{
            country: 'US',
            sayHello: 'Hello World'
        }, {
            country: 'Frank',
            sayHello: 'Bonjour le monde'
        }, {
            country: 'German',
            sayHello: 'Hallo Welt'
        }]
    });

In the above code, we have defined a new module called helloworld.controllers in addition to a new controller called HelloController.

Next, we will add this module to our existing application. Open www/js/app.js and add in this module.

Here is how the app.js code looks after adding the new helloworld.controllers module.

angular.module('helloworld', [
    'ionic',
    'helloworld.controllers'
]);

Include the controllers.js in the index.html after app.js.

We need to attach the above controller logic to our ion-list in the index.html. Modify the ion-list as shown below:

<ion-content>
    <ion-list ng-controller="HelloController">
        <ion-item ng-repeat="item in helloList">
            {{item.sayHello}}
        </ion-item>
    </ion-list>
</ion-content>

iOS & Android App Tutorial Using HTML5

Step 5: Adding Items to the List

Next, we need to implement a way to add items to the existing list. For this, we’ll make use of ionicModal which will slide up after clicking a button on the listing page.

First, add a new button on top of the listing page to add new items to the list.

Next, let’s make our header look more colorful by making use of the ion-header-bar. Modify the code as shown below:

<ion-header-bar class="bar-positive">
    <h1 class="title">Say hello to the world with Ionic</h1>
    <div class="buttons">
        <button class="button" ng-click="openAddHelloModal()"> Add </button>
    </div>
</ion-header-bar>

As you can see in the code above, we’ve added an ion-header-bar with a class bar-positive which is what gives it that color. You can have many different types of headers for this app tutorial. Here are more details on how to do so.

In addition, we have also added a new button to our header called ‘Add,’ which calls a function to open a modal window.

Almost finished with the app tutorial…

Add the modal pop up to the index.html as shown below before the </body> and move ng-controller=”HelloController” to the <body> element, so the controller also affects the <ion-header> element too. See the example code below.

<script id="modal.html" type="text/ng-template">
    <div class="modal">
        <div class="bar bar-header bar-calm">
            <button class="button" ng-click="closeModal()">Close</button>
            <h1 class="title">Add hello</h1>
        </div>
        <br></br>
        <br></br>
        <form ng-submit="AddHello(data)">
            <div class="list">
                <div class="list list-inset">
                    <label class="item item-input">
                    <input type="text" placeholder="Hello item" ng-model="data.newItem">
                </label>
                </div>
                <button class="button button-block button-positive" type="submit">Add</button>
            </div>
        </form>
    </div>
</script>

As per the code above, we now have a header inside our modal, an input box and an Add button to add new items to the to-do list.

Now we have a back button in the header, which we have attached as a closeModal() function by using ng-click to close the modal. We have attached a function called AddItem to a form using ng-submit, which has added items to the existing list on the form submit.

Our last step of this app tutorial is binding the ionic modal to our controller. Finally, we will inject $ionicModal to the controller and define the required functions as shown below:

angular.module('helloworld.controllers', [])
    .controller('HelloController', function($scope, $ionicModal) {
        $scope.helloList = [{
            country: 'US',
            sayHello: 'Hello World'
        }, {
            country: 'Frank',
            sayHello: 'Bonjour le monde'
        }, {
            country: 'German',
            sayHello: 'Hallo Welt'
        }]
        // Init the modal
        $ionicModal.fromTemplateUrl('modal.html', {
            scope: $scope,
            animation: 'slide-in-up'
        }).then(function(modal) {
            $scope.modal = modal;
        });
        // Show modal
        $scope.openAddHelloModal = function() {
            $scope.modal.show();
        };
        // Close the modal
        $scope.closeModal = function() {
            $scope.modal.hide();
        };
        // Cleanup the modal when done
        $scope.$on('$destroy', function() {
            $scope.modal.remove();
        });
        // Add new item to list
        $scope.AddHello = function(data) {
            $scope.helloList.push({
                country: 'Test',
                sayHello: data.newItem
            });
            data.newItem = '';
            $scope.closeModal();
        };
    });

iOS & Android App Tutorial Using HTML5iOS & Android App Tutorial Using HTML5

It’s always helpful to stay on top of your code writing, especially when it comes to mobile app platforms. Stay tuned for more coding and app tutorials from the team here at Pangara!

 

If you’re ready to take the next step, then let’s get started. If you’re applying as a top freelance simply click ‘Become a Pangara Talent.‘ For clients, ‘Hire a Pangara Talent.’ We look forward to working with you!

Stay tuned for Pangara’s latest news updates and events on their Facebook page, LinkedIn and Twitter.