Documente Academic
Documente Profesional
Documente Cultură
Let's Do it…
Now It is time for Ionic2(Cross Platform Mobile App Using AngularJS and Typescript). If you know
angularJS and Typescript then it is your “cup of tea”.
HTTP Services(Providers)
Fetching JSON Data From URL
Add task
Conditionally applying css class
Delete Task
Update Task & more..
You can also find the code here (h ps://github.com/jinalshah999/ionic2todoapplication.git), So feel free
to play with code yourself. In this tutorial I am using Ionic2 CLI (Command Line Interface to have work
convenience with ionic2). In other word by using cli it will create boiler plate ready for us. For setup and
all other instructions please go through the previous article here..
(h ps://jinalshahblog.wordpress.com/2016/11/19/ge ing-started-with-ionic-2-cli/)
In this tutorial I am going to make to-do application to simply list all task, add task, delete task and
update task. I had already created the REST Api Using NodeJS.
(h ps://jinalshahblog.wordpress.com/2016/10/06/rest-api-using-node-js-and-mysql/),so it will fetch data
from mysql. My Task Table Contains 3 columns Id, Title & Status.
First I had created class and named it as Task, it will be used to store the json data.
j
Let’s say for example, we require one function which can be used by more than one components. Then,
what happens, we just need to write the same code again and again for each component. When we want
to change some logic in function, then we need to change it on each and every component.
i.e. instead of doing this, we simply create a service in which we can write the code once that can be
used by as many components as we want ,by simply injecting the service Instance. In other languages,
services keeps our function and logic centralized.
Creating Provider/Service:
The above function will return all the task from the database. But before creating this function ,create the
object of HTTP as shown below and also import rxjs/Rx for map and observable.
So here in above service I had created all methods for task like getAllTask(),addTask(),deleteTask() and
more.
Important Point:
To make this service available to each component, it must be declared inside the provider array of
app.module.ts (i.e. global declaration file) as shown below.
1 import { NgModule } from '@angular/core';
2
3 import { IonicApp, IonicModule } from 'ionic-angular';
4
5 import { MyApp } from './app.component';
6
7 import { AboutPage } from '../pages/about/about';
8
9 import { ContactPage } from '../pages/contact/contact';
10
11 import { Dbtaskservice } from '../providers/dbtaskservice';
12
13 @NgModule({
14
15 declarations: [
16
17 MyApp,
18
19 AboutPage,
20
21 ContactPage
22
23 ],
24
25 providers: [Dbtaskservice]
26
27 })
Creating Component:
So far I had created class and service, now it’s turn for component to display all the task.
Ionic2 comes with global declaration concept. So, whenever creating a component, it must need to
declare it in app.module.ts, inside the declaration array and entryComponents array as shown below,
then only it can be used.
1 import { NgModule } from '@angular/core';
2
3 import { IonicApp, IonicModule } from 'ionic-angular';
4
5 import { MyApp } from './app.component';
6
7 import { AboutPage } from '../pages/about/about';
8
9 import { ContactPage } from '../pages/contact/contact';
10
11 import { TasksPage } from '../pages/tasks/tasks';
12
13 @NgModule({
14
15 declarations: [
16
17 MyApp,
18
19 AboutPage,
20
21 ContactPage,
22
23 TasksPage
24
25 ],
26
27 entryComponents: [
28
29 MyApp,
30
31 AboutPage,
32
33 ContactPage,
34
35 HomePage,
36
37 TasksPage
38
39 ],
40
41 })
1. Html
2. TypeScript
I will first start with TypeScript part. Now TypeScript can be further divide in 3 parts.
1. Import section
2. Component metadata
3. Class
First, create the array named it as allTasks which is the type of task(which is created earlier).
Then, inject the dbtaskservice inside the constructor and create the instance of our service.
And then finally, call getAllTask method of our service inside the ionViewDidLoad event.
In Above code, I had imported loadingcontroller from ionic-angular package, to show progress bar on
loading also imported toastcontroller to display the message on successfully insert or delete or update
the task. Now on html I will loop through the allTask array and display all the task. So the html will look
like following.
1 <ion-header>
2
3 <ion-navbar>
4
5 <ion-title>taskdb</ion-title>
6
7 </ion-navbar>
8
9 </ion-header>
10
11 <ion-content padding>
12
13 <ion-list inset>
14
15 <ion-input placeholder="id" autofocus="" [(ngModel)]="id" ></ion-input>
16
17 <ion-input placeholder="What needs to be done?" [(ngModel)]="title" (keyup.en
18
19 <ion-item *ngFor="let t of allTask" ><!--(click)="taskSelected(t)"-->
20
21 <ion-label [ngClass]="{'donestatus': t.Status=='done','pendingstatus':t.Status
22
23 <ion-icon item-right (click)="deleteTask(t)" ios="ios-trash" md="md-trash"></
24
25 <ion-icon item-right (click)="updateTask(t)" ios="ios-color-wand" md="md-colo
26
27 <!--<ion-icon name="trash" ios="ios-trash" md="md-trash"></ion-icon>-->
28
29 </ion-item>
30
31 </ion-list>
32
33 </ion-content>
Here in above html I am also applying the class conditionally i.e. the completed task should be displayed
in blue and uncompleted task should be displayed in red.
1 page-tasks {
2
3 .donestatus{
4
5 color: blue;
6
7 }
8
9 .pendingstatus{
10
11 color: red;
12
13 }
14
15 }
Also I had used two way binding for adding the task.
Two way binding can be achieved using ngModel. Here I created the keyup event which fired on enter
key pressed. Overall typescript will look like this after adding addTask,updateTask and deleteTask.
I had created the tab application so before running the application I must need to change the default
page i.e. go to tab page and change the home page to tasks page as shown below
1 import { Component } from '@angular/core';
2
3 import { HomePage } from '../home/home';
4
5 import { AboutPage } from '../about/about';
6
7 import { ContactPage } from '../contact/contact';
8
9 import { Tasks } from '../tasks/tasks';
10
11 @Component({
12
13 templateUrl: 'tabs.html'
14
15 })
16
17 export class TabsPage {
18
19 // this tells the tabs component which Pages
20
21 // should be each tab's root Page
22
23 tab1Root: any = Tasks;
24
25 tab2Root: any = AboutPage;
26
27 tab3Root: any = ContactPage;
28
29 constructor() {
30
31
32
33 }
34
35 }
cmd>ionic serve
to-do-application/screenshot-114/)
(h ps://jinalshahblog.wordpress.com/2017/01/18/crud-operations-in-ionic2-
to-do-application/screenshot-115/)
(h ps://jinalshahblog.wordpress.com/2017/01/18/crud-operations-in-ionic2-to-
do-application/screenshot-116/)
(h ps://jinalshahblog.wordpress.com/2017/01/18/crud-operations-in-ionic2-to-
do-application/screenshot-117/)
(h ps://jinalshahblog.wordpress.com/2017/01/18/crud-operations-in-ionic2-to-do-application/screenshot-
118/)
Summary
Ionic2 is simply awesome. Isn’t it?
Ionic-CLI
Created class
Services
Components
Run time css class
Add,Update and Delete and more…
There is a lot more to come in the next part of the tutorial. So, stay tuned for more.
Advertisements
i am freelance trainer on different technology like node.Js, Angular2, BOT Framework etc. i had done my
B.E. (Computer) from C.U. Shah College of Engg. & Tech., Wadhwan City, Surendranagar in year 2009. i
am running own coaching classes in Ahmedabad, Gujarat for different I.T. subjects & also co-founder in
Brainoorja Creations. h p://brainoorja.com i am actively associated with academics and trainings to I.T.
students of various colleages studying in B.E., B.Sc. (IT), B.C.A./M.C.A. my latest blogs and workshop
details are published at: h ps://jinalshahblog.wordpress.com my core expertise lies in imparting
trainings on Angular2 and hands-on experience on other web technologies like, ASP.NET, MVC , C#
NET, Android. my hobbies are research, development and trainings in upcoming web & mobile
technologies. i can be reached at: +91 98258 89888 and jinalshah999@gmail.com View all posts by Jinal Shah
hello sir please tell how to use restful php api with mysql in ionic 2