Javascript 保存/编辑跨度时显示当前日期(angularjs)
我使用Javascript 保存/编辑跨度时显示当前日期(angularjs),javascript,angularjs,time,Javascript,Angularjs,Time,我使用angularjs创建了一个相当简单的任务列表,我希望在创建每个任务时节省时间,并在编辑任务时更新该时间 我想我可以用一些东西来显示当前时间,但我不知道如何在保存/编辑时显示 HTML: 只需在todo模型中添加一个todoDate属性,如 $scope.todos.push({text: $scope.todoText, done: false, editing: false, todoDate: new Date()}); 并在用户更新todo对象iesave() 希望对您有所帮助。
angularjs
创建了一个相当简单的任务列表,我希望在创建每个任务时节省时间,并在编辑任务时更新该时间
我想我可以用一些东西来显示当前时间,但我不知道如何在保存/编辑时显示
HTML:
只需在todo模型中添加一个
todoDate
属性,如
$scope.todos.push({text: $scope.todoText, done: false, editing: false, todoDate: new Date()});
并在用户更新todo对象iesave()
希望对您有所帮助。当您调用保存功能添加新TODO时,只需添加createdOn字段即可
var app = angular.module('todoApp', []);
app.controller('TodoListController', ['$scope', function ($scope) {
$scope.todos = [];
$scope.newField = [];
$scope.customStyle = {};
$scope.addTodo = function () {
$scope.todos.push({text: $scope.todoText, done: false, editing: false, created: new Date()});
$scope.todoText = '';
};
$scope.remaining = function () {
var count = 0;
angular.forEach($scope.todos, function (todo) {
count += todo.done ? 0 : 1;
});
return count;
};
$scope.delete = function () {
var oldTodos = $scope.todos;
$scope.todos = [];
angular.forEach(oldTodos, function (todo) {
if (!todo.done) $scope.todos.push(todo);
});
};
$scope.remove = function () {
$scope.todos.splice(this.$index, 1);
};
$scope.change = function (field) {
var todoIndex = $scope.todos.indexOf(field);
$scope.newField[todoIndex] = angular.copy(field);
$scope.todos[todoIndex].editing = true;
$scope.todos[todoIndex].created = new Date();
};
$scope.save = function (index) {
$scope.todos[index].editing = false;
$scope.todos[index].created = new Date();
};
$scope.cancel = function (index) {
$scope.todos[index] = $scope.newField[index];
};
$scope.updateVar = function (event) {
$scope.myVar = angular.element(event.target).text();
};
$scope.editKeyword = function (name, index) {
$scope.mode[index] = 'edit';
console.log(name);
};
}]);
$scope.save = function (index) {
$scope.todos[index].editing = false;
$scope.todos[index].createdOn = new Date().getTime();
};
现在,当用户编辑todo并调用change函数时,请执行以下操作
$scope.change = function (field) {
var todoIndex = $scope.todos.indexOf(field);
$scope.newField[todoIndex] = angular.copy(field);
$scope.todos[todoIndex].editing = true;
$scope.todos[todoIndex].LastModifyOn = new Date().getTime();
};
现在,如果用户再次编辑此更新的todo,我们只需调用change函数,它将更新LastModifyOn字段
因此,通过这样做,我们可以保留两个数据,比如todo是何时创建的,最后一次是何时更新的。那么问题出在哪里?@Jax问题是将我的想法与现有代码结合在一起。我不知道该怎么办it@RicardoPimenta你找到解决办法了吗。我的解决方案是否在任何意义上帮助了你或回答了你的问题?我也对这个问题投了赞成票。问题是,所有任务都与第一次创建任务的时间相同,即使在编辑之后,编辑后的最后一次修改将对特定的todo进行更改,而createdOn不会更改。
var app = angular.module('todoApp', []);
app.controller('TodoListController', ['$scope', function ($scope) {
$scope.todos = [];
$scope.newField = [];
$scope.customStyle = {};
$scope.addTodo = function () {
$scope.todos.push({text: $scope.todoText, done: false, editing: false, created: new Date()});
$scope.todoText = '';
};
$scope.remaining = function () {
var count = 0;
angular.forEach($scope.todos, function (todo) {
count += todo.done ? 0 : 1;
});
return count;
};
$scope.delete = function () {
var oldTodos = $scope.todos;
$scope.todos = [];
angular.forEach(oldTodos, function (todo) {
if (!todo.done) $scope.todos.push(todo);
});
};
$scope.remove = function () {
$scope.todos.splice(this.$index, 1);
};
$scope.change = function (field) {
var todoIndex = $scope.todos.indexOf(field);
$scope.newField[todoIndex] = angular.copy(field);
$scope.todos[todoIndex].editing = true;
$scope.todos[todoIndex].created = new Date();
};
$scope.save = function (index) {
$scope.todos[index].editing = false;
$scope.todos[index].created = new Date();
};
$scope.cancel = function (index) {
$scope.todos[index] = $scope.newField[index];
};
$scope.updateVar = function (event) {
$scope.myVar = angular.element(event.target).text();
};
$scope.editKeyword = function (name, index) {
$scope.mode[index] = 'edit';
console.log(name);
};
}]);
$scope.save = function (index) {
$scope.todos[index].editing = false;
$scope.todos[index].createdOn = new Date().getTime();
};
$scope.change = function (field) {
var todoIndex = $scope.todos.indexOf(field);
$scope.newField[todoIndex] = angular.copy(field);
$scope.todos[todoIndex].editing = true;
$scope.todos[todoIndex].LastModifyOn = new Date().getTime();
};