Ionic 2 | Tutorial | Let’s Create Our First Application
http://www.gajotres.net/ionic-2-tutorial-lets-create-our-first-application/
By
Gajotres -Last updated on March 31st, 2016
In my previous article, I showed you an anatomy of Ionic 2 page. Now let’s do something with that knowledge, we’re going to create our first Ionic 2 application, and we’re going to create something useful. Our application will use a Master-Detail pattern and REST protocol (We will use it to query international movie database).
Note: If this tutorial was helpful, need further clarification, something is not working or do you have a request for another Ionic post? Furthermore, if you don't like something about this blog, if something is
bugging you, don't like how I'm doing stuff here, again leave me a comment below. I'm here to
help you, I expect the same from you. Feel free to comment below, subscribe to my blog, mail me to
dragan.gaic@gmail.com, or follow and mention me on twitter (@gajotres). Thanks and have a nice day!
PS. If you want my help, if possible (even if it takes you some time to do that), create a working example I can play with. Use Plunker for AngularJS based questions or jsFiddle for jQuery/jQuery Mobile based questions.
PS. If you want my help, if possible (even if it takes you some time to do that), create a working example I can play with. Use Plunker for AngularJS based questions or jsFiddle for jQuery/jQuery Mobile based questions.
Table of Contents
Preparations
- Android Environment (or iOS if you’re working on a MacOS)
- nodeJS
- Ionic 2
- Cordova
1. Update Ionic & Cordova
1
|
npm install
-g cordova ionic@2.0.0-beta.22 |
1
|
npm update -g cordova ionic@2.0.0-beta.22 |
Warning: Ionic2 is still in beta so the final implementation may differ from this code. I will update this article if and when that happens.
2. Create A New Project
1
2
|
ionic start Ionic2FirstApp blank --v2 cd
Ionic2FirstApp |
Warning: Since some of you never worked with Ionic CLI. From this point and further, every time I tell you to execute something, do that inside a project folder.
1
|
ionic platform add android |
1
|
ionic platform add ios |
3. Install Cordova Whitelist Plugin
1
|
cordova plugin add cordova-plugin-whitelist |
1
|
< meta
http-equiv = "Content-Security-Policy"
content = "default-src *; script-src 'self' 'unsafe-inline' 'unsafe-eval' *; style-src 'self' 'unsafe-inline' *" > |
Example

Embeded example:
Ionic Page Navigation
Source Code
Update: 31.03.2016 (March 31st) - Article and example are updated to match changes made to Ionic Beta 22 version

index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<! DOCTYPE
html> < html
lang = "en" > < head > < title >Ionic</ title > < meta
charset = "UTF-8" > < meta
name = "viewport"
content = "width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" > < meta
http-equiv = "Content-Security-Policy"
content = "default-src *; script-src 'self' 'unsafe-inline' 'unsafe-eval' *; style-src 'self' 'unsafe-inline' *" > < link
ios-href = "build/css/app.ios.css"
rel = "stylesheet" > < link
md-href = "build/css/app.md.css"
rel = "stylesheet" > < link
wp-href = "build/css/app.wp.css"
rel = "stylesheet" >
</ head > < body > < ion-app ></ ion-app > <!-- cordova.js required for cordova apps --> < script
src = "cordova.js" ></ script > <!-- Zone.js and Reflect-metadata --> < script
src = "build/js/angular2-polyfills.js" ></ script > <!-- the bundle which is built from the app's source code --> < script
src = "build/js/app.bundle.js" ></ script > </ body > </ html > |
1
|
< ion-app ></ ion-app > |
app.html
1
|
< ion-nav
id = "nav"
[root]="rootPage" #content swipe-back-enabled = "false" ></ ion-nav > |
1
|
[root]="rootPage" |
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
import
'es6-shim' ; import
{App, IonicApp, Platform} from 'ionic-angular' ; import
{StatusBar} from 'ionic-native' ; import
{MovieListPage} from './pages/movie-list/movie-list' ; @App({ templateUrl:
'build/app.html' , config: {} }) class
MyApp { static
get parameters() { return
[[IonicApp], [Platform]]; } constructor(app, platform) { this .app = app; this .platform = platform; this .initializeApp(); this .rootPage = MovieListPage; } initializeApp() { this .platform.ready().then(() => { StatusBar.styleDefault(); }); } } |
1
2
3
4
|
import
'es6-shim' ; import
{App, IonicApp, Platform} from 'ionic-angular' ; import
{StatusBar} from 'ionic-native' ; import
{MovieListPage} from './pages/movie-list/movie-list' ; |
1
2
3
4
5
6
|
@App({ templateUrl:
'build/app.html' , config: {} }) class
MyApp { |
1
|
this .rootPage = MovieListPage; |
http://www.gajotres.net/ionic-2-tutorial-lets-create-our-first-application/2/
app.core.scss
@import "../pages/movie-list/movie-list";
@import "../pages/movie-info/movie-info";
movie-list.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | < ion-navbar *navbar> < ion-title >Movie List</ ion-title > </ ion-navbar > < ion-content padding> < ion-list > < ion-header > < ion-item > < ion-input type="text" placeholder="Search a movie..." (input)="searchMovieDB($event, searchKey)"></ ion-input > </ ion-item > </ ion-header > < ion-item *ngFor="#movie of movies" (click)="itemTapped($event, movie)"> < ion-avatar item-left> < img src="https://image.tmdb.org/t/p/w92{{movie.poster_path}}"/> </ ion-avatar > < h2 >{{movie.original_title}}</ h2 > < p class="item-description">{{movie.overview}}</ p > </ ion-item > </ ion-list > </ ion-content > |
1 2 3 4 5 | < ion-header > < ion-item > < ion-input type="text" placeholder="Search a movie..." (input)="searchMovieDB($event, searchKey)"></ ion-input > </ ion-item > </ ion-header > |
1 | (input)="searchMovieDB($event, searchKey) |
1 2 3 | < ion-item *ngFor="#movie of movies" (click)="itemTapped($event, movie)"> </ ion-item > |
1 | *ngFor="#movie of movies" |
1 2 3 4 5 | < ion-avatar item-left> < img src="https://image.tmdb.org/t/p/w92{{movie.poster_path}}"/> </ ion-avatar > < h2 >{{movie.original_title}}</ h2 > < p class="item-description">{{movie.overview}}</ p > |
movie-list.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | import {Page, NavController} from 'ionic-angular' ; import {MovieService} from '../services/MovieService' ; import {MovieInfo} from '../movie-info/movie-info' ; @Page({ templateUrl: 'build/pages/movie-list/movie-list.html' , providers: [MovieService] }) export class MovieListPage { static get parameters() { return [[NavController], [MovieService]]; } constructor(nav, movieService) { this .nav = nav; this .movieService = movieService; } searchMovieDB(event, key) { if (event.target.value.length > 2) { this .movieService.searchMovies(event.target.value).subscribe( data => { this .movies = data.results; console.log(data);}, err => this .logError(err), () => console.log( 'Movie Search Complete' ) ); } } itemTapped(event, movie) { this .nav.push(MovieInfo, { movie: movie }); } } |
1 2 3 | import {Page, NavController} from 'ionic-angular' ; import {MovieService} from '../services/MovieService' ; import {MovieInfo} from '../movie-info/movie-info' ; |
1 2 3 4 | @Page({ templateUrl: 'build/pages/movie-list/movie-list.html' , providers: [MovieService] }) |
EXCEPTION: No provider for MovieService! (MovieListPage -> MovieService)
1 2 3 4 5 6 7 8 | static get parameters() { return [[NavController], [MovieService]]; } constructor(nav, movieService) { this .nav = nav; this .movieService = movieService; } |
1 2 3 4 5 6 7 8 9 | searchMovieDB(event, key) { if (event.target.value.length > 2) { this .movieService.searchMovies(event.target.value).subscribe( data => { this .movies = data.results; console.log(data);}, err => this .logError(err), () => console.log( 'Movie Search Complete' ) ); } } |
1 2 3 4 5 | itemTapped(event, movie) { this .nav.push(MovieInfo, { movie: movie }); } |
MovieService.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import {Http} from 'angular2/http' ; import 'rxjs/add/operator/map' ; export class MovieService { static get parameters() { return [[Http]]; } constructor(http) { this .http = http } searchMovies(movieName) { var url = 'http://api.themoviedb.org/3/search/movie?query=&query=' + encodeURI(movieName) + '&api_key=5fbddf6b517048e25bc3ac1bbeafb919' ; this .response = this .http.get(url).map(res => res.json()); return this .response; } } |
1 2 | import {Http} from 'angular2/http' ; import 'rxjs/add/operator/map' ; |
movie-info.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | import {IonicApp, Page, NavController, NavParams} from 'ionic-angular' ; @Page({ templateUrl: 'build/pages/movie-info/movie-info.html' }) export class MovieInfo { static get parameters() { return [[IonicApp], [NavController], [NavParams]]; } constructor(app, nav, navParams) { this .nav = nav; this .movie = navParams.get( 'movie' ); console.log( this .movie); } } |
1 | this .movie = navParams.get( 'movie' ); |
movie-info.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | < ion-navbar *navbar> < ion-title >Movie Details</ ion-title > </ ion-navbar > < ion-content > < div *ngIf="movie" class="selection"> < ion-card > < ion-item > < ion-avatar item-left image-large> < img src="https://image.tmdb.org/t/p/w92{{movie.poster_path}}"/> </ ion-avatar > < ion-item-content class="movie-title-data"> < h1 >{{movie.title}}</ h1 > < p >{{movie.release_date}}</ p > </ ion-item-content > </ ion-item > < ion-item > < icon document item-left></ icon > < h2 >Overview</ h2 > < p class="item-description">{{movie.overview}}</ p > </ ion-item > < ion-item > < icon bookmark item-left></ icon > < h2 >Average Vote</ h2 > < p >{{movie.vote_average}}</ p > </ ion-item > </ ion-card > </ div > </ ion-content > |
Deployment
1 | ionic build android |
1 | ionic run android -l -c -s |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通