我正在使用带有更新功能的离子框架制作待办事项列表应用。 每当我们点击一个项目时,一个新的更新模式弹出并要求更新。我已经做到这一点但不知道如何使其正常运行(意味着如何实际更新项目)。 我在互联网上搜索过,但没有找到任何有用的信息。 我分享了index.html,app.js和contollers.js的代码 的index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Todo</title>
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<link href="lib/ionic/css/ionic.css" rel="stylesheet">
<script src="lib/ionic/js/ionic.bundle.js"></script>
<!-- Needed for Cordova/PhoneGap (will be a 404 during development) -->
<script src="cordova.js"></script>
<script src="js/app.js"></script>
</head>
<body ng-app="todo" ng-controller="TodoCtrl">
<ion-side-menus>
<!-- Center content -->
<ion-side-menu-content drag-content="true">
<ion-header-bar class="bar-royal">
<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="true">
<ion-list>
<ion-item ng-repeat="task in activeProject.tasks" >
<div ng-click="updateTask()">
{{task.title}}
</div>
</ion-item>
</ion-list>
</ion-content>
</ion-side-menu-content>
<!-- Left menu -->
<ion-side-menu side="left" width="150">
<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="true">
<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>
//Create Task
<script id="new-task.html" type="text/ng-template">
<div class="modal">
<!-- Modal header bar -->
<ion-header-bar class="bar-secondary">
<h1 class="title">Add 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 want to do?" ng-model="task.title">
</label>
</div>
<div class="padding">
<button type="submit" class="button button-block button-dark">Create Task</button>
</div>
</form>
</ion-content>
</div>
</script>
<!-- Update task here -->
<script id="update-task.html" type="text/ng-template">
<div class="modal">
<!-- Modal header bar -->
<ion-header-bar class="bar-secondary">
<h1 class="title">Update Task</h1>
<button class="button button-clear button-positive" ng-click="closeUpdateTask()">Cancel</button>
</ion-header-bar>
<!-- Modal content area -->
<ion-content>
<form ng-submit="UpdateTask(task)">
<div class="list">
<label class="item item-input">
<input type="text" placeholder="Edit your stuff here?" ng-model="task.title">
</label>
</div>
<div class="padding">
<button type="submit" class="button button-block button-dark">Update Task</button>
</div>
</form>
</ion-content>
</div>
</script>
</ion-side-menus>
</body>
</html>
app.js
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.taskModal1 = modal;
}, {
scope: $scope
});
// Create our modal
$ionicModal.fromTemplateUrl('update-task.html', function(modal) {
$scope.taskModal2 = modal;
}, {
scope: $scope
});
$scope.createTask = function(task) {
if(!$scope.activeProject || !task) {
return;
}
$scope.activeProject.tasks.push({
title: task.title
});
$scope.taskModal1.hide();
// Inefficient, but save all the projects
Projects.save($scope.projects);
task.title = "";
};
$scope.UpdateTask = function(task) {
};
$scope.newTask = function() {
$scope.taskModal1.show();
};
$scope.updateTask = function() {
$scope.taskModal2.show();
};
$scope.closeNewTask = function() {
$scope.taskModal1.hide();
}
$scope.closeUpdateTask = function() {
$scope.taskModal2.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;
}
}
}
});
});
controllers.js
angular.module('todo', ['ionic'])
.controller('TodoCtrl', function($scope, $ionicModal) {
// No need for testing data anymore
$scope.tasks = [];
// Create and load the Create task Modal
$ionicModal.fromTemplateUrl('update-task.html', function(modal) {
id: 1;
$scope.taskModal1 = modal;
}, {
scope: $scope,
animation: 'slide-in-up'
});
// Create and load the Update Task Modal
$ionicModal.fromTemplateUrl('update-task.html', function(modal) {
id: 2;
$scope.taskModal2 = modal;
}, {
scope: $scope,
animation: 'slide-in-up'
});
// Called when the form is submitted
$scope.createTask = function(task) {
$scope.tasks.push({
title: task.title
});
$scope.taskModal1.hide();
task.title = "";
};
// Called when the task is to be updated
$scope.UpdateTask = function(task) {
};
// Open our new task modal
$scope.newTask = function() {
$scope.taskModal1.show();
};
// Open our update task modal
$scope.updateTask = function() {
$scope.taskModal2.show();
};
// Close the new task modal
$scope.closeNewTask = function() {
$scope.taskModal.hide();
};
});
有人可以告诉我如何更新任务???
//在要更新任务时调用
$scope.UpdateTask = function(task) {
//.............
};
答案 0 :(得分:0)
这是您可以遵循的步骤:
当用户选择任务时,将索引保存到临时范围变量中,并将该任务复制到其他变量[currentTask]中。
$scope.UpdateTask = function(index) {
$scope.currentIndex = index;
$scope.currentTask = angular.copy($scope.tasks[$index]);
};
在打开的表单中,所有模型字段都应绑定到currentTask。
<form ng-submit="saveTask()">
<div class="list">
<label class="item item-input">
<input type="text" placeholder="Edit your stuff here?" ng-model="currentTask.title">
</label>
</div>
<div class="padding">
<button type="submit" class="button button-block button-dark">Update Task</button>
</div>
</form>
使用索引更新值。
$scope.sveTask = function(index) {
if ($scope.$scope.currentIndex === -1) {
$scope.tasks.push(angular.copy($scope.tasks[$index]));
} else {
$scope.tasks[$scope.currentIndex] = angular.copy($scope.tasks[$index]);
}
$scope.currentIndex = -1;
$scope.currentTask = {};
};