【发布时间】:2014-07-11 20:40:11
【问题描述】:
我有一个简单的应用程序,可以将一个项目添加到列表中,但该项目被添加到列表的底部而不是顶部。有没有办法让它添加到顶部,例如预先添加它?让我知道您是否需要查看更多代码。任何帮助是极大的赞赏。谢谢。
HTML
<!-- LOOP OVER THE TODOS IN $scope.todos -->
<div class="checkbox" ng-repeat="todo in todos">
<label>
<input type="checkbox" ng-click="deleteTodo(todo._id)"> {{ todo.text }}
</label>
</div>
一些 Angular.js
// when submitting the add form, send the text to the node API
$scope.createTodo = function() {
$scope.loading = true;
// validate the formData to make sure that something is there
// if form is empty, nothing will happen
if ($scope.formData.text != undefined) {
// call the create function from our service (returns a promise object)
Todos.create($scope.formData)
// if successful creation, call our get function to get all the new todos
.success(function(data) {
$scope.loading = false;
$scope.formData = {}; // clear the form so our user is ready to enter another
$scope.todos = data; // assign our new list of todos
});
}
};
// create todo and send back all todos after creation
app.post('/api/todos', function(req, res) {
// create a todo, information comes from AJAX request from Angular
Todo.create({
text : req.body.text,
done : false
}, function(err, todo) {
if (err)
res.send(err);
// get and return all the todos after you create another
Todo.find(function(err, todos) {
if (err)
res.send(err)
res.json(todos);
});
});
});
【问题讨论】:
-
todos.unshift(todo) -
我要把它放在我的 createTodo 函数中吗?
-
一直说todos没有定义。
标签: javascript arrays angularjs list prepend