利用方法としてはnpmでionic、cordova、ios-simをインストール。
sudo npm install -g ionic cordova ios-simionicコマンドでプロジェクトを開始します
ionic start myproject blank途中で「Create an ionic.io account to send Push Notifications and use the Ionic View app?」と聞かれるのでひとまずnoで。
作成が終了したらプロジェクト内に移動してionic serveで起動するとブラザでプロジェクトが確認できる。
cd myproject
ionic serve初期表示画面のHTMLは「/myproject/www/index.html」にあるのでこちらを修正してリロードすれば修正内容が反映されている。
MacではiOSシミュレータで確認ができる。初回のみ「ionic platform add ios」でビルドすれば以下のコマンドでiOSシュミレータで確認出来ます。
ionic platform add ios//(初回のみ)
ionic build ios
ionic emulate iosでは、Ionic Framewarkを利用してTODO アプリを作っていきましょう。(参考:Ionic Book: Table of Contents - Ionic Framework)
index.htmlは初期時は以下のようになっています。
<!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">
<!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
<link href="css/ionic.app.css" rel="stylesheet">
-->
<!-- ionic/angularjs js -->
<script src="lib/ionic/js/ionic.bundle.js"></script>
<!-- cordova script (this will be a 404 during development) -->
<script src="cordova.js"></script>
<!-- your app's js -->
<script src="js/app.js"></script>
</head>
<body ng-app="starter">
<ion-pane>
<ion-header-bar class="bar-stable">
<h1 class="title">Ionic Blank Starter</h1>
</ion-header-bar>
<ion-content>
</ion-content>
</ion-pane>
</body>
</html>サイドメニューを表示する
まずは、body要素の内側を以下のように変更します。
<ion-side-menus>
<ion-side-menu-content></ion-side-menu-content>
<ion-side-menu side="left"></ion-side-menu>
</ion-side-menus>そうするとドラッグ(スワイプ)でサイドメニューが表示されるようになります。
ヘッダーを表示する
更に以下のように変更します。
<ion-side-menus>
<!-- Center content -->
<ion-side-menu-content>
<ion-header-bar class="bar-dark">
<h1 class="title">Todo</h1>
</ion-header-bar>
<ion-content>
</ion-content>
</ion-side-menu-content>
<!-- Left menu -->
<ion-side-menu side="left">
<ion-header-bar class="bar-dark">
<h1 class="title">Projects</h1>
</ion-header-bar>
</ion-side-menu>
</ion-side-menus>メインコンテツ、サイドメニューそれぞれにヘッダーが表示されます。
リストを表示する
次に<ion-content>内を以下のように変更し、
<ion-content>
<ion-list>
<ion-item ng-repeat="task in tasks">
{{task.title}}
</ion-item>
</ion-list>
</ion-content>body要素を以下のように変更し、
<body ng-app="starter" ng-controller="TodoCtrl">app.jsを以下のように変更するとメインコンテツにリストが表示されます。
&angular.module('starter', ['ionic'])
.controller('TodoCtrl', function($scope) {
$scope.tasks = [
{ title: 'Collect coins' },
{ title: 'Eat mushrooms' },
{ title: 'Get high enough to grab the flag' },
{ title: 'Find the Princess' }
];
});TODOを登録できるようにする
次にページ内のモーダルでTODOを登録できるように修正しましょう。index.html上に以下のHTMLをついきます。
<script id="new-task.html" type="text/ng-template">
<div class="modal">
<!-- Modal header bar -->
<ion-header-bar class="bar-secondary">
<h1 class="title">New Task</h1>
<button class="button button-clear button-positive" ng-click="closeNewTask()">Cancel</button>
</ion-header-bar>
<!-- Modal content area -->
<ion-content>
<form ng-submit="createTask(task)">
<div class="list">
<label class="item item-input">
<input type="text" placeholder="What do you need to do?" ng-model="task.title">
</label>
</div>
<div class="padding">
<button type="submit" class="button button-block button-positive">Create Task</button>
</div>
</form>
</ion-content>
</div>
</script>更に<ion-header-bar>のh1の次に以下の様な<button>を追加します。
<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>最後にapp.jsのcontroller('TodoCtrl')を以下のように変更しましょう。
angular.module('starter', ['ionic'])
.controller('TodoCtrl', function($scope, $ionicModal) {
// No need for testing data anymore
$scope.tasks = [];
// Create and load the Modal
$ionicModal.fromTemplateUrl('new-task.html', function(modal) {
$scope.taskModal = modal;
}, {
scope: $scope,
animation: 'slide-in-up'
});
// Called when the form is submitted
$scope.createTask = function(task) {
$scope.tasks.push({
title: task.title
});
$scope.taskModal.hide();
task.title = "";
};
// Open our new task modal
$scope.newTask = function() {
$scope.taskModal.show();
};
// Close the new task modal
$scope.closeNewTask = function() {
$scope.taskModal.hide();
};
});こうすることで右上にボタンが表示され、そのボタンタップ時にモーダルが表示されモーダル内でTODOが登録できるようになります。
プロジェクトの追加
では、最後にプロジェクトで管理できるようにしましょう。
<ion-side-menu-content>を以下のように変更
<!-- Center content -->
<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>
<!-- New Task button-->
<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>を以下のように変更
<!-- Left menu -->
<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>最後にapp.jsを以下のように変更しましょう。
angular.module('starter', ['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;
}
}
}
});
});これで、完成。
Angularを触れるのが大前提のフレームワークですね。
【開発環境・ツールの最新記事】
- Webpack2を試す
- axiosを試してみた。
- RequireJS 入門
- MacでMongoDBを利用する
- Sublime Text2で⌘+dの逆
- Pallarelsにmodan.IEをイ..
- CoffeeScriptの基本
- Sublime Text2のEmmet用..
- Alfredの使い方
- 静的サイトジェネレータ「Middlema..
- Sublime Text 2にインストー..
- Sublime Text 2のショートカ..
- VirtualBoxを利用して無料のIE..
- FlashからCreateJSに変換した..
- AppKitBoxでAndroidを楽..
- ターミナルでディレクトリ移動
- Sublime Text 2でブラウザリ..
- Sublime Text 2でブラウザプ..
- Sublime Text 2でファイルを..
- CodeKitでHaml
