ionic Creating an APP
In the previous chapter we learned how to import the ionic framework into a project.
Next we will introduce how to create an ionic APP application.
Creating an ionic APP uses HTML, CSS, and Javascript to build, so we can create a www directory and create an index.html file in it, with the following code:
Todo
In the above code, we imported the Ionic CSS file, the Ionic JS file, and the Ionic AngularJS extension ionic.bundle.js (ionic.bundle.js).
The ionic.bundle.js file already contains the Ionic core JS, AngularJS, and the Ionic AngularJS extension; if you need to import other Angular modules, you can call them from the lib/js/angular directory.
cordova.js is generated when creating an app with Cordova/PhoneGap and does not need to be imported manually; you can find this file in Cordova/PhoneGap projects, so showing a 404 during development is normal.
Creating an APP
Next we will implement an app with a title, sidebar, list, etc.; the design is as follows:
Creating a Sidebar
The sidebar is created using the ion-side-menus controller.
Edit the index.html file we created earlier and modify the content inside , as follows:
Controller analysis:
- ion-side-menus: is a container with a sidebar menu; the menu can be expanded by tapping a button or swiping the screen.
- ion-side-menu-content: a container that displays the main content; the menu can be expanded by swiping the screen.
- ion-side-menu: a container that holds the sidebar.
Initializing the APP
Next we create a new AngularJS module and initialize it; the code is in www/js/app.js:
angular.module('todo', ['ionic'])
Then add the ng-app attribute to our body tag:
In the index.html file's import the app.js file above:
Modify the content of the body tag in the index.html file, with the following code:
Todo
Projects
With the above steps we have completed the basic ionic APP application.
Creating a list
First create a controller TodoCtrl:
In the app.js file, use a controller to define the list data:
angular.module('todo', ['ionic'])
.controller('TodoCtrl', function($scope) {
$scope.tasks = [
{ title: 'Runoob Tutorial' },
{ title: 'www.runoob.com' },
{ title: 'Runoob Tutorial' },
{ title: 'www.runoob.com' }
];
});
In the index.html page we use Angular ng-repeat to iterate over the data list:
Todo
{{task.title}}
The modified code inside the index.html body tag is as follows:
Todo
{{task.title}}
Projects
Creating an Add Page
Modify index.html at add the following code after :
In the above code we use Defines a new template page.
The createTask(task) function is triggered when the form is submitted.
ng-model="task.title" assigns the form's input data to the title property of the task object.
Modify
<!-- 中心内容 -->
<ion-side-menu-content>
<ion-header-bar class="bar-dark">
<h1 class="title">Todo</h1>
<!-- 新增按钮-->
<button class="button button-icon" ng-click="newTask()">
<i class="icon ion-compose"></i>
</button>
</ion-header-bar>
<ion-content>
<!-- 列表 -->
<ion-list>
<ion-item ng-repeat="task in tasks">
{{task.title}}
</ion-item>
</ion-list>
</ion-content>
</ion-side-menu-content>
In the app.js file, the controller code is as follows:
angular.module('todo', ['ionic'])
.controller('TodoCtrl', function($scope, $ionicModal) {
$scope.tasks = [
{ title: 'Runoob Tutorial' },
{ title: 'www.runoob.com' },
{ title: 'Runoob Tutorial' },
{ title: 'www.runoob.com' }
];
// Create and load model
$ionicModal.fromTemplateUrl('new-task.html', function(modal) {
$scope.taskModal = modal;
}, {
scope: $scope,
animation: 'slide-in-up'
});
// Called when submitting the form
$scope.createTask = function(task) {
$scope.tasks.push({
title: task.title
});
$scope.taskModal.hide();
task.title = "";
};
// Open new model
$scope.newTask = function() {
$scope.taskModal.show();
};
// Disable new models
$scope.closeNewTask = function() {
$scope.taskModal.hide();
};
});
Creating a Sidebar
Through the steps above we have implemented the add feature; now we add a sidebar feature to the app.
Modify
The content inside , the code is as follows:
<!-- 中心内容 -->
<ion-side-menu-content>
<ion-header-bar class="bar-dark">
<button class="button button-icon" ng-click="toggleProjects()">
<i class="icon ion-navicon"></i>
</button>
<h1 class="title">{{activeProject.title}}</h1>
<!-- 新增按钮 -->
<button class="button button-icon" ng-click="newTask()">
<i class="icon ion-compose"></i>
</button>
</ion-header-bar>
<ion-content scroll="false">
<ion-list>
<ion-item ng-repeat="task in activeProject.tasks">
{{task.title}}
</ion-item>
</ion-list>
</ion-content>
</ion-side-menu-content>
Add a sidebar:
<!-- 左边栏 -->
<ion-side-menu side="left">
<ion-header-bar class="bar-dark">
<h1 class="title">Projects</h1>
<button class="button button-icon ion-plus" ng-click="newProject()">
</button>
</ion-header-bar>
<ion-content scroll="false">
<ion-list>
<ion-item ng-repeat="project in projects" ng-click="selectProject(project, $index)" ng-class="{active: activeProject == project}">
{{project.title}}
</ion-item>
</ion-list>
</ion-content>
</ion-side-menu>
Here we create a new js file app2.js (to avoid confusion with the previous one), with the code as follows:
angular.module('todo', ['ionic'])
/**
* The Projects factory handles saving and loading projects
* from local storage, and also lets us save and load the
* last active project index.
*/
.factory('Projects', function() {
return {
all: function() {
var projectString = window.localStorage['projects'];
if(projectString) {
return angular.fromJson(projectString);
}
return [];
},
save: function(projects) {
window.localStorage['projects'] = angular.toJson(projects);
},
newProject: function(projectTitle) {
// Add a new project
return {
title: projectTitle,
tasks: []
};
},
getLastActiveIndex: function() {
return parseInt(window.localStorage['lastActiveProject']) || 0;
},
setLastActiveIndex: function(index) {
window.localStorage['lastActiveProject'] = index;
}
}
})
.controller('TodoCtrl', function($scope, $timeout, $ionicModal, Projects, $ionicSideMenuDelegate) {
// A utility function for creating a new project
// with the given projectTitle
var createProject = function(projectTitle) {
var newProject = Projects.newProject(projectTitle);
$scope.projects.push(newProject);
Projects.save($scope.projects);
$scope.selectProject(newProject, $scope.projects.length-1);
}
// Load or initialize projects
$scope.projects = Projects.all();
// Grab the last active, or the first project
$scope.activeProject = $scope.projects[Projects.getLastActiveIndex()];
// Called to create a new project
$scope.newProject = function() {
var projectTitle = prompt('Project name');
if(projectTitle) {
createProject(projectTitle);
}
};
// Called to select the given project
$scope.selectProject = function(project, index) {
$scope.activeProject = project;
Projects.setLastActiveIndex(index);
$ionicSideMenuDelegate.toggleLeft(false);
};
// Create our modal
$ionicModal.fromTemplateUrl('new-task.html', function(modal) {
$scope.taskModal = modal;
}, {
scope: $scope
});
$scope.createTask = function(task) {
if(!$scope.activeProject || !task) {
return;
}
$scope.activeProject.tasks.push({
title: task.title
});
$scope.taskModal.hide();
// Inefficient, but save all the projects
Projects.save($scope.projects);
task.title = "";
};
$scope.newTask = function() {
$scope.taskModal.show();
};
$scope.closeNewTask = function() {
$scope.taskModal.hide();
}
$scope.toggleProjects = function() {
$ionicSideMenuDelegate.toggleLeft();
};
// Try to create the first project, make sure to defer
// this by using $timeout so everything is initialized
// properly
$timeout(function() {
if($scope.projects.length == 0) {
while(true) {
var projectTitle = prompt('Your first project title:');
if(projectTitle) {
createProject(projectTitle);
break;
}
}
}
});
});
The ion-side-menus code in body is as follows::
<ion-side-menus>
<!-- 中心内容 -->
<ion-side-menu-content>
<ion-header-bar class="bar-dark">
<button class="button button-icon" ng-click="toggleProjects()">
<i class="icon ion-navicon"></i>
</button>
<h1 class="title">{{activeProject.title}}</h1>
<!-- 新增按钮 -->
<button class="button button-icon" ng-click="newTask()">
<i class="icon ion-compose"></i>
</button>
</ion-header-bar>
<ion-content scroll="false">
<ion-list>
<ion-item ng-repeat="task in activeProject.tasks">
{{task.title}}
</ion-item>
</ion-list>
</ion-content>
</ion-side-menu-content>
<!-- 左边栏 -->
<ion-side-menu side="left">
<ion-header-bar class="bar-dark">
<h1 class="title">Projects</h1>
<button class="button button-icon ion-plus" ng-click="newProject()">
</button>
</ion-header-bar>
<ion-content scroll="false">
<ion-list>
<ion-item ng-repeat="project in projects" ng-click="selectProject(project, $index)" ng-class="{active: activeProject == project}">
{{project.title}}
</ion-item>
</ion-list>
</ion-content>
</ion-side-menu>
</ion-side-menus>