1. 程式人生 > >Ionic動態載入頁面

Ionic動態載入頁面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link href="lib/ionic/css/ionic.css" rel="stylesheet">

    <script src="lib/ionic/js/ionic.bundle.js"></script>
    <script src="js/app.js"></script>
<script type="text/javascript" src="cordova.js"></script> </head> <body ng-app="todo" ng-controller="TodoCtrl"> <ion-side-menus> <!-- 中心內容 --> <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> <!-- 左側選單 --> <ion-side-menu side="left"> <ion-header-bar class="bar-dark"> <h1 class="title">Left</h1> </ion-header-bar> </ion-side-menu> </ion-side-menus> <script id="new-task" type="text/ng-template"> <div class="modal"> <ion-header-bar class="bar-secondary"> <h1 class="title">新建事務</h1> <button class="button button-clear button-positive" ng-click="closeNewTask()">關閉</button> </ion-header-bar> <ion-content> <form ng-submit="createTask(task)"> <div class="list"> <label class="item item-input"> <input type="text" placeholder="請填寫代辦事項" ng-model="task.title"> </label> </div> <div class="padding"> <button type="submit" class="button button-block button-positive">新建事務</button> </div> </form> </ion-content> </div> </script> </body> </html>

app.js

angular.module('todo', ['ionic'])

        .controller('TodoCtrl', function ($scope, $ionicModal) {
        $scope.tasks = [
            {title: 'aaa'},
            {title: 'bbb'},
            {title: 'ccc'},
            {title: 'ddd'}
        ];
        // 建立並載入模型
        $ionicModal.fromTemplateUrl('new-task', function (modal) {
            $scope.taskModal = modal;
        }, {
            scope: $scope,
            animation: 'slide-in-up'
        });

        // 表單提交時呼叫
        $scope.createTask = function (task) {
            $scope.tasks.push({
                title: task.title
            });
            $scope.taskModal.hide();
            task.title = "";
        };

        // 開啟新增的模型
        $scope.newTask = function () {
            $scope.taskModal.show();
        };

        // 關閉新增的模型
        $scope.closeNewTask = function () {
            $scope.taskModal.hide();
        };

    });

$ionicModal.fromTemplateUrl用於載入html片段,第一個引數對應script的id。第二引數是一個方法,該方法的引數對應div片段的class。這裡將整個html片段賦值給頁面的一個叫taskModal的變數,點選新建/關閉按鈕的時候通過show()或hide()來控制彈出框的顯示和隱藏。點選新建事務後將輸入框輸入的值通過push追加到tasks陣列中,這樣就可以在列表中回顯剛新增的資料。

ionic寫的頁面能自適應螢幕
這裡寫圖片描述

這裡寫圖片描述