2016-03-07 69 views
0

我目前面臨顯示離子模式(1.7.14)的問題。我創建了一個包含列表和添加按鈕的簡單應用程序。在這個按鈕上,我想展示一個在我的任務列表中添加新元素的模式。Ionic無法顯示模態

這裏是我的示例代碼

modal.html(通過www /模板文件夾中創建新的文件)

<ion-modal-view> 
    <!-- Modal header bar --> 
    <ion-header-bar class="bar-secondary"> 
     <h1 class="title">New Task</h1> 
    </ion-header-bar> 

    <!-- Modal content area --> 
    <ion-content> 
     <button class="button" ng-click="closeModal()">back</button> 
     <form ng-submit="AddItem(data)"> 
     <div class="list"> 
      <label class="item item-input"> 
      <input type="text" placeholder="What do you need to do?" ng-model="data.newItem"> 
      </label> 
     </div> 
     <div class="padding"> 
      <button type="submit" class="button button-block button-positive">Add item</button> 
     </div> 
     </form> 

    </ion-content> 
</ion-modal-view> 

controller.js文件

angular.module('starter.controllers', []) 

.controller('ToDoListCtrl', function ($scope, $ionicModal) { 

// array list which will contain the items added 
$scope.toDoListItems = [{ 
task: 'First task', 
status: 'not done' 
}, { 
task: 'Second task', 
status: 'not done' 
}]; 

//init the modal 
$ionicModal.fromTemplateUrl('templates/modal.html', { 
    scope: $scope, 
    animation: 'slide-in-up' 
}).then(function (modal) { 
    $scope.modal = modal; 
}); 

// function to open the modal 
$scope.openModal = function() { 
    $scope.modal.show(); 
}; 

// function to close the modal 
$scope.closeModal = function() { 
    $scope.modal.hide(); 
}; 

//Cleanup the modal when we're done with it! 
$scope.$on('$destroy', function() { 
    $scope.modal.remove(); 
}); 

//function to add items to the existing list 
$scope.AddItem = function (data) { 
    $scope.toDoListItems.push({ 
     task: data.newItem, 
     status: 'not done' 
    }); 
    data.newItem = ''; 
    $scope.closeModal(); 
}; 

}); 

的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> 
<script src="js/controller.js"></script> 

</head> 
<!--<body ng-app="starter">--> 
<body ng-app="starter"> 
    <ion-header-bar class="bar-positive"> 
    <h1 class="title">Todo list</h1> 
    <div class="buttons"> 
     <button class="button icon ion-plus" ng-click="openModal()"> </button> 
    </div> 
    </ion-header-bar> 
    <ion-content> 
     <ion-list ng-controller="ToDoListCtrl"> 
     <ion-item ng-repeat="item in toDoListItems"> 
      {{item.task}} 
     </ion-item> 
    </ion-list> 
    </ion-content> 
</body> 
</html> 

個app.js

angular.module('starter', ['ionic', 'starter.controllers']) 
.run(function ($ionicPlatform) { 
$ionicPlatform.ready(function() { 
if (window.StatusBar) { 
    StatusBar.styleDefault(); 
} 
}); 
}) 

我試圖打開模式之前添加的console.log(「測試」),並登錄命令不會在控制檯diplayed ...

回答

0

的問題是,您將您的控制器到<ion-list>中的視圖,並且該按鈕是之前創建的,因此它不起作用。在創建按鈕之前,您必須添加ng-controller="ToDoListCtrl"

+0

您的意思是在身體標籤? – gsoulie

+0

是的,'ng-app'後 – ddepablo

+0

是的,它的工作!非常感謝。 – gsoulie

0

您已將控制器添加到列表項中,但您試圖在列表中的控制器外調用該功能。 所以試試這個。

<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> 
<script src="js/controller.js"></script> 

</head> 
<!--<body ng-app="starter">--> 
<body ng-app="starter"> 
    <ion-header-bar class="bar-positive"> 
    <h1 class="title">Todo list</h1> 
    <div class="buttons"> 
     <button class="button icon ion-plus" ng-click="openModal()"> </button> 
    </div> 
    </ion-header-bar> 
    <ion-content ng-controller="ToDoListCtrl"> 
     <ion-list> 
     <ion-item ng-repeat="item in toDoListItems"> 
      {{item.task}} 
     </ion-item> 
    </ion-list> 
    </ion-content> 
</body> 
</html>