ng--todolist
todolist小案例
走外国服务器, ̄□ ̄||
app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { FormsModule } from '@angular/forms';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
FormsModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
核心组件app.component.ts

发现这俩不是同一类文件,哈哈哈,٩(๑>◡<๑)۶
import { Component } from '@angular/core';
const todos = [
{
id: 1,
title: '吃饭',
done: true
},
{
id: 2,
title: '唱歌',
done: false
},
{
id: 3,
title: '写代码',
done: true
}
]
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
public todos: {
id: number,
title: string,
done: boolean
}[] = JSON.parse(window.localStorage.getItem('todos') || '[]')
public visibility: string = 'all'
public currentEditing: {
id: number,
title: string,
done: boolean
} = null
// 该函数是一个特殊的 Angular 生命周期钩子函数
// 它会在 Angular 应用初始化的时候执行一次
ngOnInit () {
// 初始化的时候手动调用一次
this.hashchangeHandler()
// 注意:这里要 bind this绑定
window.onhashchange = this.hashchangeHandler.bind(this)
}
// 当 Angular 组件数据发生改变的时候,ngDoCheck 钩子函数会被触发
// 我们要做的就是在这个钩子函数中去持久化存储我们的 todos 数据
ngDoCheck() {
window.localStorage.setItem('todos', JSON.stringify(this.todos))
}
get filterTodos () {
if (this.visibility === 'all') {
return this.todos
} else if (this.visibility === 'active') {
return this.todos.filter(t => !t.done)
} else if (this.visibility === 'completed') {
return this.todos.filter(t => t.done)
}
}
// 实现导航切换数据过滤的功能
// 1. 提供一个属性,该属性会根据当前点击的链接返回过滤之后的数据
// filterTodos
// 2. 提供一个属性,用来存储当前点击的链接标识
// visibility 字符串
// all、active、completed
// 3. 为链接添加点击事件,当点击导航链接的时候,改变
//
addTodo (e): void {
const titleText = e.target.value
if (!titleText.length) {
return
}
const last = this.todos[this.todos.length - 1]
this.todos.push({
id: last ? last.id + 1: 1,
title: titleText,
done: false
})
// 清除文本框
e.target.value = ''
}
get toggleAll () {
return this.todos.every(t => t.done)
}
set toggleAll (val: boolean) {
this.todos.forEach(t => t.done = val)
}
removeTodo (index: number): void {
this.todos.splice(index, 1)
}
saveEdit (todo, e) {
// 保存编辑
todo.title = e.target.value
// 去除编辑样式
this.currentEditing = null
}
handleEditKeyUp (e) {
const {keyCode, target} = e
if (keyCode === 27) {
// 取消编辑
// 同时把文本框的值恢复为原来的值
target.value = this.currentEditing.title
this.currentEditing = null
}
}
get remaningCount () {
return this.todos.filter(t => !t.done).length
}
hashchangeHandler () {
// 当用户点击了锚点的时候,我们需要获取当前的锚点标识
// 然后动态的将根组件中的 visibility 设置为当前点击的锚点标识
const hash = window.location.hash.substr(1)
switch (hash) {
case '/':
this.visibility = 'all'
break;
case '/active':
this.visibility = 'active'
break;
case '/completed':
this.visibility = 'completed'
break;
}
}
// 清除所有已完成任务项
clearAllDone () {
this.todos = this.todos.filter(t => !t.done)
}
}
模板app.component.html
<section class="todoapp">
<header class="header">
<h1>todos</h1>
<input
class="new-todo"
placeholder="What needs to be done?"
autofocus
(keyup.enter)="addTodo($event)">
</header>
<ng-template [ngIf]="todos.length">
<!-- This section should be hidden by default and shown when there are todos -->
<section class="main">
<input
id="toggle-all"
class="toggle-all"
type="checkbox"
(change)="toggleAll = $event.target.checked"
[checked]="toggleAll">
<label for="toggle-all">Mark all as complete</label>
<ul class="todo-list">
<!-- These are here just to show the structure of the list items -->
<!-- List items should get the class `editing` when editing and `completed` when marked as completed -->
<!--
li 是每一个任务项
每个任务项有三种状态:
正常状态 没有样式
完成状态 completed
编辑状态 editing
-->
<li
*ngFor="let todo of filterTodos; let i = index;"
[ngClass]="{
completed: todo.done,
editing: currentEditing === todo
}">
<div class="view">
<input
class="toggle"
type="checkbox"
[(ngModel)]="todo.done">
<label (dblclick)="currentEditing = todo">{{ todo.title }}</label>
<button
class="destroy"
(click)="removeTodo(i)"></button>
</div>
<input
class="edit"
[value]="todo.title"
(keyup)="handleEditKeyUp($event)"
(keyup.enter)="saveEdit(todo, $event)"
(blur)="saveEdit(todo, $event)">
</li>
</ul>
</section>
<!-- This footer should hidden by default and shown when there are todos -->
<footer class="footer">
<!-- This should be `0 items left` by default -->
<span class="todo-count"><strong>{{ remaningCount }}</strong> item left</span>
<!-- Remove this if you don't implement routing -->
<ul class="filters">
<li>
<a [ngClass]="{
selected: visibility === 'all'
}" href="#/">All</a>
</li>
<li>
<a [ngClass]="{
selected: visibility === 'active'
}" href="#/active">Active</a>
</li>
<li>
<a [ngClass]="{
selected: visibility === 'completed'
}" href="#/completed">Completed</a>
</li>
</ul>
<!-- Hidden if no completed items are left ↓ -->
<button
(click)="clearAllDone()"
class="clear-completed">Clear completed</button>
</footer>
</ng-template>
</section>
<footer class="info">
<p>Double-click to edit a todo</p>
<!-- Remove the below line ↓ -->
<p>Template by <a href="http://sindresorhus.com">Sindre Sorhus</a></p>
<!-- Change this out with your name and url ↓ -->
<p>Created by <a href="http://todomvc.com">you</a></p>
<p>Part of <a href="http://todomvc.com">TodoMVC</a></p>
</footer>
ng--todolist的更多相关文章
- 【码在江湖】前端少侠的json故事(中)ng的json
ng的json 正所谓"人在江湖,身不由己",在开发之路上前端少侠dk遇到过种种困难,尤其在与后端进行数据对接的时候,不得不逼迫自己以极快的速度去学习和掌握一些奇招怪式,正当他以为 ...
- Vue.js基础篇实战--一个ToDoList小应用
距离开始学Vue已经过去一个多月了,总想把学到的东西柔和在一起,做点东西出来,于是有了这个Todolist小应用. 使用vuex 纯粹基础,没有用到web pack,vuex,npm,下次把它改造一下 ...
- 重写官方TodoList,对于初学react+redux的人来说,很有好处
虽然官网的TodoList的例子写的很详细,但是都是一步到位,就是给你一个action,好家伙,全部都写好了,给你一个reducer,所有功能也是都写好了,但是我们这些小白怎么可能一下就消化那么多,那 ...
- [vue案例的知识点]todo-list
文章的原材料来自于vue的官方示例:https://cn.vuejs.org/v2/examples/todomvc.html,我们在学习过程中,试着对其中的一些知识点进行记录: 一.浏览器数据存储, ...
- 不知道张(zhāng)雱(pāng)是谁?你out了!
张(zhāng)雱(pāng)是谁?也许你已经听说过了,也许你还没听说过呢,不过你一定听说过老刘——刘强东,没错,这二人是有关系的,什么关系,京东是老刘的,而张雱呢?张雱是京东旗下52家关联公司法人代 ...
- Flume NG Getting Started(Flume NG 新手入门指南)
Flume NG Getting Started(Flume NG 新手入门指南)翻译 新手入门 Flume NG是什么? 有什么改变? 获得Flume NG 从源码构建 配置 flume-ng全局选 ...
- matlab基础教程——根据Andrew Ng的machine learning整理
matlab基础教程--根据Andrew Ng的machine learning整理 基本运算 算数运算 逻辑运算 格式化输出 小数位全局修改 向量和矩阵运算 矩阵操作 申明一个矩阵或向量 快速建立一 ...
- 汇编语言标志位 含义 NV UP EI NG NZ AC PE CY
缩写原意: Overflow of = OV NV [No Overflow] Direction df = DN (decrement) UP (increment) Interrupt if = ...
- 走进AngularJs(二) ng模板中常用指令的使用方式
通过使用模板,我们可以把model和controller中的数据组装起来呈现给浏览器,还可以通过数据绑定,实时更新视图,让我们的页面变成动态的.ng的模板真是让我爱不释手.学习ng道路还很漫长,从模板 ...
- 第一次部署Struts2时出现错误java.lang.ClassNotFoundException: org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.class
报如下错误 at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720) at org. ...
随机推荐
- Codeforces_713_A
http://codeforces.com/problemset/problem/713/A 将读入的数全都存为二进制,用map保存对应的个数. #include<iostream> #i ...
- 2018icpc南京现场赛-G Pyramid(打标找规律+逆元)
题意: 求n行三角形中等边三角形个数,图二的三角形也算. n<=1e9 思路: 打表找下规律,打表方法:把所有点扔坐标系里n^3爆搜即可 打出来为 1,5,15,35,70,126,210.. ...
- Why Oracle VIP can not be switched to original node ?
Oracle RAC is an share everything database architecture. The article is how to check out why virtual ...
- 编写SQL语句(快速回顾)
注:源自于<Java程序员面试秘笈>! 1.创建数据库MYDB create database MYDB 2.创建学生表student (sno,sname,ssex,sage,sclas ...
- 2020牛客寒假算法基础集训营4 -- A : 欧几里得
A:欧几里得 考察点 : 递推, gcd 坑点 : long long 这道题题解说的十分详细,是裴波那契的一种变形,只不过换成 gcd 了. Code: #include <cstdio> ...
- vue的组件传值
1.父组件向子组件传值 父组件: 123456789101112 <template> <child :name="name"></child> ...
- 浅谈无线局域网WLAN
无线局域网WLAN 一.概述 有线局域网的组成如下图所示,多台计算机通过双绞线连接到一个集线器(hub)或交换机(switch)上,组成一个有限局域网. 无线局域网的组成如下图所示,多台计算机通过无线 ...
- sed知识及常用用法梳理
1.sed命令简介及其参数说明 sed流编辑器,擅长对文本进行增删改查,过滤指定的字符串和取指定行,也可以在行中字符串前后插入内容,功能非常强大. 注意:sed默认只支持基本的正则表达式,如果要想支持 ...
- iptables服务器主机防火墙
iptables参数说明: Commands: Either long or short options are allowed. --append -A chain 链尾部追加一条规则 --dele ...
- [Redis-CentOS7]Redis安装(-)
系统环境 CentOS Linux release 7.7.1908 (Core) yum安装 yum install redis Loaded plugins: fastestmirror Load ...