简体   繁体   English

保存/编辑跨度时显示当前日期(AngularJS)

[英]Display current date when saving/editing span (angularjs)

I have a fairly simple task list using angularjs and I would like to save the time when each task was created and update that time if the task is edited. 我有一个使用angularjs的相当简单的任务列表,我想节省创建每个任务的时间,并在编辑任务时更新该时间。

I'm thinking I could use something like this to display the current time, but I'm not sure how to do it on save/edit. 我想我可以使用类似这样的东西来显示当前时间,但是我不确定如何在保存/编辑时做到这一点。

HTML: HTML:

<div ng-controller="TodoListController">
  <form ng-submit="addTodo()" name="form">
      <input type="text" ng-model="todoText" size="30" placeholder="Add New Entry" required id="textField" ng-model="myVar">
      <input class="btn-primary" type="submit" value="Save">
  </form>
  <ul class="unstyled">
    <li ng-repeat="todo in todos | orderBy : $index:true">
      <button type="button" class="close" aria-label="Close" ng-click="remove(todo)">
        <span aria-hidden="true">&times;</span>
      </button>
      <span class="done-{{todo.done}}" ng-style="todo.customStyle" ng-hide="todo.editing" ng-click="updateVar($event)">{{todo.text}}</span>
      <input type="text" ng-show="todo.editing" ng-model="todo.text">
      <button type="submit" ng-hide="todo.editing" ng-click="change(todo); todo.editing === true">Edit</button>

      <button type="submit" ng-show="todo.editing" ng-click="save($index); todo.editing === false">Save</button>
      <button type="submit" ng-show="todo.editing" ng-click="cancel($index); todo.editing === false">Cancel</button>
    </li>
  </ul>
</div>

JS: JS:

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});
    $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.save = function (index) {
    $scope.todos[index].editing = false;
  };
  $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);
  };
}]);

Just add a todoDate attribute in the todo model like 只需在todo模型中添加todoDate属性,例如

$scope.todos.push({text: $scope.todoText, done: false, editing: false, todoDate: new Date()});

and update it when the user updates the todo object ie save() 并在用户更新todo对象(即save()时对其进行更新

$scope.save = function (index) {
    $scope.todos[index].editing = false;
    $scope.todos[index].todoDate = new Date();
};

Hope it helps. 希望能帮助到你。

When you call save function to add new todos simply add the createdOn field. 当您调用保存功能添加新的待办事项时,只需添加createdOn字段即可。

$scope.save = function (index) {
    $scope.todos[index].editing = false;
    $scope.todos[index].createdOn = new Date().getTime();
};

Now when user edits the todo and change function is called, do as below 现在,当用户编辑待办事项并更改功能时,请执行以下操作

$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();
  };

Now if this updated todo is again edited by the user we need to simply call change function and it will update the LastModifyOn filed. 现在,如果用户再次编辑了此更新的待办事项,我们只需要简单地调用change函数,它将更新LastModifyOn字段

So by doing this, we can keep both data like when was the todo created and when was it last gets updated. 因此,通过执行此操作,我们可以保留两个数据,例如待办事项的创建时间和最后一次更新的时间。

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);
  };
}]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM