使用AngularJS将当前日期添加到帖子中
Add current date to a post with AngularJS
我正在为我的作业创建一个日记应用程序,并且正在使用AngularJS。我的问题是我如何提交当前日期以及他们写的条目。
因此,我想要完成的是,在与{{todo.title}}相同的页面上,还将显示添加新待办事项的日期。谁能帮忙?我一无所知:(
<!doctype html>
<html ng-app="To_Do">
<head>
<meta charset="UTF-8">
<title>Dear Diary</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />
<link href="stijl.css" rel="stylesheet" type="text/css">
<!--<link href="bootstrap.css" rel="stylesheet" type="text/css">-->
<link rel="shortcut icon" href="image/favicon_me.ico" type="image/x-icon" />
<link rel="apple-touch-icon" href="image/apple-icon-precomposed.png" />
<link rel="apple-touch-icon" sizes="72x72" href="image/apple-icon-72x72-precomposed.png" />
<link rel="apple-touch-icon" sizes="114x114" href="image/apple-icon-114x114-precomposed.png" />
<link rel="apple-touch-icon" sizes="144x144" href="image/apple-icon-144x144-precomposed.png" />
<link rel="apple-touch-startup-image" href="image/apple-startup-320x460.png" media="(device-width: 320px)" />
<link rel="apple-touch-startup-image" href="image/apple-startup-640x920.png" media="(device-width: 320px) and (-webkit-device-pixel-ratio: 2)" />
</head>
<body>
<header>
<h1> Dear Diary ... </h1>
</header>
<div id="container">
<div ng-controller="todoCtrl" ng-show="tab === 1">
<ul class="dots">
<li ng-repeat="todo in todos | orderBy:'date':true">
<input type="checkbox" ng-model="todo.done" />
<span ng-class="{'done':todo.done}">{{todo.title}} </span>
</li>
</ul>
<button ng-click="clearCompleted()">Clear entry</button>
</div>
<div ng-controller="todoCtrl" ng-show="tab === 2">
<p> Write to me: </p>
<form name="frm" ng-submit="addTodo()">
<input type="text" placeholder="Today was..." class="sizeText" name="newTodo" ng-model- instant ng-model="newTodo" required />
<br><br>
<button class="btn" ng-disabled="frm.$invalid"><i class="icon-plus"></i>GO</button>
</form>
<br>
</div>
</div>
<footer>
<section ng-init="tab = 1">
<ul class="nav">
<li> <a href ng-click="tab = 1"> Overzicht </a> </li>
<li> <a href ng-click="tab = 2"> Toevoegen </a> </li>
</ul>
</section>
</footer>
<script src="javascript/angular.min.js"></script>
<script>
angular.module('To_Do',['storageService']).
controller('todoCtrl',['$scope','getLocalStorage', function($scope, getLocalStorage){
$scope.todos = getLocalStorage.getTodos();
$scope.today = new Date();
[
<!-- {'title' : 'Build a todo app', date: $scope.today, 'done':false} -->
];
$scope.addTodo = function() {
$scope.todos.push({'title':$scope.newTodo , date: $scope.today, 'done':false})
getLocalStorage.updateTodos($scope.todos);
$scope.newTodo = ''
};
$scope.clearCompleted = function() {
$scope.todos = $scope.todos.filter(function(item){
return !item.done
});
getLocalStorage.updateTodos($scope.todos);
};
}]);
var storageService = angular.module('storageService', []);
storageService.factory('getLocalStorage', function() {
var todoList = {};
return {
list: todoList,
updateTodos: function (todosArr) {
if (window.localStorage && todosArr) {
localStorage.setItem("todos", angular.toJson(todosArr));
}
//update the cached version
todoList = todosArr;
},
getTodos: function () {
todoList = angular.fromJson( localStorage.getItem("todos") );
return todoList ? todoList : [];
}
};
});
</script>
</body>
</html>
每当添加新的待办事项时,请保存当前时间:
$scope.addTodo = function() {
$scope.todos.push({'title':$scope.newTodo , date: new Date(), 'done':false})
....
};
然后,如果您想在视图中显示它,请使用 angular 中的内置日期过滤器
<span> {{todo.date | date:'medium'}}</span>
相关文章:
- angularjs移除焦点上的活动类并添加到下一个项目
- 动态添加AngularJS脚本
- 向AngularJS目录添加JS变量
- 在AngularJs中添加轮询机制
- Angularjs在当前行位置添加行
- AngularJS:在.post()之后添加事件侦听器
- AngularJs,在没有隔离的情况下将指令属性添加到作用域
- 将项添加到 AngularJS 中的值服务
- AngularJS:为ng-repeat中的图像添加切换功能
- 如何在打字稿的控制器中添加 angularjs 服务
- AngularJS - ui.router - 如何在动态添加它们后重定向到所需的状态
- 在 AngularJS 表格中添加的行上显示 3 秒的弹出窗口
- 如何检测是否已使用 angular.forEach AngularJS 添加对象
- 在现有Node.js、Express、MongoDB和Socket.io Stack之上添加AngularJS
- 使用$http动态添加angularjs指令
- 如何有条件地动态添加AngularJS指令到元素中?
- 在Html中添加angularjs
- 像添加AngularJS属性一样添加ng-click
- 如何在视图中添加AngularJs的迭代值
- 在ng repeat中添加AngularJS过滤器