我正在使用angular js,我很难弄清楚如何更新一个值。我试过了,但没有成功。我会张贴我的js文件,如果有人知道任何关于我的问题帮助将不胜感激。我遇到问题的只是updateTask。
function TodoCtrl($scope) {
$scope.todos = [
{text:'task1', text2:'Mow the lawn', selected:false},
{text:'task2', text2:'Wash the car', selected:false}
];
$scope.getTotalTodos = function () {
return $scope.todos.length;
};
$scope.addTask = function () {
$scope.todos.push({text:$scope.formTodoName, text2:$scope.formTodoDescription, selected:false});
$scope.formTodoName = '';
$scope.formTodoDescription = '';
};
$scope.removeTask = function () {
$scope.todos = _.filter($scope.todos, function(todo){
return !todo.selected;
});
};
$scope.updateTask = function () {
if ($scope.todos.selected:true){
$scope.todos.put({text:$scope.formTodoName, text2:$scope.formTodoDescription, selected:false});
$scope.formTodoText = '';
};
};
}
index . html
<!DOCTYPE html>
<html >
<head>
<meta charset="UTF-8">
<title>Todo App</title>
<header>Todo App</header>
<link rel="stylesheet" href="css/reset.css">
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<html ng-app>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
<script src="//use.edgefonts.net/vast-shadow:n4:all.js"></script>
<script src="//use.edgefonts.net/vast-shadow:n4:all;megrim.js"></script>
</head>
<body>
<div class="todo-wrapper" ng-controller="TodoCtrl">
<h2>You have <span class="emphasis">{{getTotalTodos()}}</span> tasks</h2>
<ul>
<li ng-repeat="todo in todos">
<input type="checkbox" ng-model="todo.selected"/>
<span class="selected-{{todo.selected}}">{{todo.id}} {{todo.text}}: {{todo.text2}} {{todo.date_created}}</span>
</li>
</ul>
<form>
<input class="add-input" placeholder="task name" type="text" ng-model="formTodoName" ng-model-instant />
<input class="add-input2" placeholder="task decription" type="text" ng-model="formTodoDescription" ng-model-instant />
<button class="add-btn" ng-click="addTask()"><h2>Add</h2></button>
</form>
<form>
<input type="text" ng-model="task.text"></input>
<button class="update-btn" ng-click="updateTask()"><h3>Update Task</h3></button>
<button class="clear-btn" ng-click="removeTask()">Remove Task</button>
</div>
</body>
</html>
<script src='http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/underscore-min.js'></script>
<script src="js/index.js"></script>
</body>
</html>
您提到您的todos确实从您的后端具有id
属性。
$scope.todos = [
{id: 1, text:'task1', text2:'Mow the lawn', selected:false},
{id: 2, text:'task2', text2:'Wash the car', selected:false}
];
然后更新其中一个,
$scope.updateTask = function (task) {
// search $scope.todos for the item to update
var indexOfTask;
for(var i=0;i<$scope.todos.length;i++) {
if($scope.todos[i].id===task.id) indexOfTask = i;
}
// update the todo
$scope.todos[indexOfTask] = todo;
};
您还没有发布表单HTML,但是您需要在提交处理程序中传递正在更新的任务。
<form ...>
...
<input type="text" ng-model="task.text"></input>
<input type="submit" ng-click="updateTask(task)"></input>
</form>
编辑更新表单输入和提交按钮以传递对象:
<input class="add-input" placeholder="task name" type="text" ng-model="task.text" ng-model-instant />
<input class="add-input2" placeholder="task decription" type="text" ng-model="task.text2" ng-model-instant />
...
<button class="add-btn" ng-click="addTask(task)"><h2>Add</h2></button>