この記事は、英語版アシアルブログの翻訳記事です。
(原文はこちら)
==============================
ほとんどのモバイルアプリでは複数のページ間でデータの共有・受け渡しを必要とします。
OnsenUIはAngularJSの機能を利用しながらページ間でのデータ受け渡しを実現しています。
実際のアプリでは以下のように作ることができます。
※詳しい作成方法についてはこのチュートリアルで紹介されています
新しいプロジェクトを作成するとこのようになります:
ここでは以下のようにdata.jsというファイルを作成します:
var myApp = angular.module('myApp');
myApp.factory('Data', function(){
var data = {};
data.items = [
{
title: 'Item 1',
description: 'Item 1 Description'
},
{
title: 'Item 2',
description: 'Item 2 Description'
},
{
title: 'Item 3',
description: 'Item 3 Description'
}
];
return data;
});
以下のようにpage1.jsというファイルを作成します:
function Page1Controller($scope, Data){ // 1
$scope.items = Data.items; // 2
$scope.showDetail = function(index){ // 3
var selectedItem = Data.items[index];
Data.selectedItem = selectedItem;
$scope.ons.navigator.pushPage('page2.html', selectedItem.title);
}
}
Replace page1.html with the following content:
以下のようにpage1.htmlを書き換えて下さい:
<div class="page center" ng-controller="Page1Controller"> <!-- 1 -->
<ons-list>
<ons-list-item ng-repeat="item in items" ng-click="showDetail($index)"> <!-- 2 -->
{{item.title}} <!-- 3 -->
</ons-list-item>
</ons-list>
</div>
index.htmlに以下の2行を書き加えてください。
<script src="data.js"></script>
<script src="page1.js"></script>
<!doctype html>
<html lang="en" ng-app="myApp">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<link rel="stylesheet" href="plugins/plugin-loader.css">
<script type="text/javascript" src="plugins/plugin-loader.js"></script>
<script>
angular.module('myApp', ['onsen.directives']);
</script>
<script src="data.js"></script>
<script src="page1.js"></script>
</head>
<body>
<ons-screen page="home_navigator.html"></ons-screen>
</body>
</html>
page1.htmlの一覧でリストが選択された時、page2.htmlに選択された項目の詳細を表示させましょう。
serviceの「Data」には page1.jsで定義したshowDetail()関数で選択したアイテムを格納していたことを思い出しましょう。
ここでは「Data」に格納した項目を使って詳細画面を表示します。
function Page2Controller($scope, Data){
$scope.item = Data.selectedItem;
}
以下をindex.htmlに追記してください。
<script src="page2.js"></script>
<!doctype html>
<html lang="en" ng-app="myApp">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<link rel="stylesheet" href="plugins/plugin-loader.css">
<script type="text/javascript" src="plugins/plugin-loader.js"></script>
<script>
angular.module('myApp', ['onsen.directives']);
</script>
<script src="data.js"></script>
<script src="page1.js"></script>
<script src="page2.js"></script>
</head>
<body>
<ons-screen page="home_navigator.html"></ons-screen>
</body>
</html>
page2.htmlを以下のように書き換えて下さい:
<div class="page center" ng-controller="Page2Controller"> <!-- 1 -->
<h2>{{item.title}}</h2> <!-- 2 -->
<p>{{item.description}}</p><!-- 2 -->
</div>
実際にアプリでテストしてみましょう。
page1.htmlでItem1を選択するとpage2.htmlに遷移し、以下のように表示されます:
index.html
<!doctype html>
<html lang="en" ng-app="myApp">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<link rel="stylesheet" href="plugins/plugin-loader.css">
<script type="text/javascript" src="plugins/plugin-loader.js"></script>
<script>
angular.module('myApp', ['onsen.directives']);
</script>
<script src="data.js"></script>
<script src="page1.js"></script>
<script src="page2.js"></script>
</head>
<body>
<ons-screen page="home_navigator.html"></ons-screen>
</body>
</html>
var myApp = angular.module('myApp');
myApp.factory('Data', function(){
var data = {};
data.items = [
{
title: 'Item 1',
description: 'Item 1 Description'
},
{
title: 'Item 2',
description: 'Item 2 Description'
},
{
title: 'Item 3',
description: 'Item 3 Description'
}
];
return data;
});
function Page1Controller($scope, Data){
$scope.items = Data.items;
$scope.showDetail = function(index){
var selectedItem = Data.items[index];
Data.selectedItem = selectedItem;
$scope.ons.navigator.pushPage('page2.html', selectedItem.title);
}
}
<div class="page center" ng-controller="Page1Controller">
<ons-list>
<ons-list-item ng-repeat="item in items" ng-click="showDetail($index)">
{{item.title}}
</ons-list-item>
</ons-list>
</div>
function Page2Controller($scope, Data){
$scope.item = Data.selectedItem;
}
{{item.description}}