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的更多相关文章

  1. 【码在江湖】前端少侠的json故事(中)ng的json

    ng的json 正所谓"人在江湖,身不由己",在开发之路上前端少侠dk遇到过种种困难,尤其在与后端进行数据对接的时候,不得不逼迫自己以极快的速度去学习和掌握一些奇招怪式,正当他以为 ...

  2. Vue.js基础篇实战--一个ToDoList小应用

    距离开始学Vue已经过去一个多月了,总想把学到的东西柔和在一起,做点东西出来,于是有了这个Todolist小应用. 使用vuex 纯粹基础,没有用到web pack,vuex,npm,下次把它改造一下 ...

  3. 重写官方TodoList,对于初学react+redux的人来说,很有好处

    虽然官网的TodoList的例子写的很详细,但是都是一步到位,就是给你一个action,好家伙,全部都写好了,给你一个reducer,所有功能也是都写好了,但是我们这些小白怎么可能一下就消化那么多,那 ...

  4. [vue案例的知识点]todo-list

    文章的原材料来自于vue的官方示例:https://cn.vuejs.org/v2/examples/todomvc.html,我们在学习过程中,试着对其中的一些知识点进行记录: 一.浏览器数据存储, ...

  5. 不知道张(zhāng)雱(pāng)是谁?你out了!

    张(zhāng)雱(pāng)是谁?也许你已经听说过了,也许你还没听说过呢,不过你一定听说过老刘——刘强东,没错,这二人是有关系的,什么关系,京东是老刘的,而张雱呢?张雱是京东旗下52家关联公司法人代 ...

  6. Flume NG Getting Started(Flume NG 新手入门指南)

    Flume NG Getting Started(Flume NG 新手入门指南)翻译 新手入门 Flume NG是什么? 有什么改变? 获得Flume NG 从源码构建 配置 flume-ng全局选 ...

  7. matlab基础教程——根据Andrew Ng的machine learning整理

    matlab基础教程--根据Andrew Ng的machine learning整理 基本运算 算数运算 逻辑运算 格式化输出 小数位全局修改 向量和矩阵运算 矩阵操作 申明一个矩阵或向量 快速建立一 ...

  8. 汇编语言标志位 含义 NV UP EI NG NZ AC PE CY

    缩写原意: Overflow of = OV NV [No Overflow] Direction df = DN (decrement) UP (increment) Interrupt if = ...

  9. 走进AngularJs(二) ng模板中常用指令的使用方式

    通过使用模板,我们可以把model和controller中的数据组装起来呈现给浏览器,还可以通过数据绑定,实时更新视图,让我们的页面变成动态的.ng的模板真是让我爱不释手.学习ng道路还很漫长,从模板 ...

  10. 第一次部署Struts2时出现错误java.lang.ClassNotFoundException: org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.class

    报如下错误 at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720) at org. ...

随机推荐

  1. 微信小程序—Flex布局

    参考教程:http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html     https://xluos.github.io/demo/flexb ...

  2. Codeforces 1092 F Tree with Maximum Cost (换根 + dfs)

    题意: 给你一棵无根树,每个节点有个权值$a_i$,指定一个点u,定义$\displaystyle value = \sum^v a_i*dist(u,v)$,求value的最大值 n,ai<= ...

  3. oracle怎么建立本地连接

    sqlplus连接oracle数据库(连接本地oracle数据库和连接远程的oracle数据库) 虽然我们现在平时都是使用PLSQL Developer这个软件工具了,但是我们还是要了解sqlplus ...

  4. JAVA 调用控件开发

    最近homoloCzh有个小伙伴接到一个需求说是把一个c# 写的具备扫描.调阅等功能 winfrom 影像控件嵌入到java Swing当中,让小伙伴很苦恼啊,从年前一直研究到年后,期间用了很多种方法 ...

  5. EF core (code first) 通过自动迁移实现多租户数据分离 :按Schema分离数据

    前言 本文是多租户系列文章的附加操作文章,如果想查看系列中的其他文章请查看下列文章 主线文章 Asp.net core下利用EF core实现从数据实现多租户(1) Asp.net core下利用EF ...

  6. istio-ServiceMesh解决方案

    istio-ServiceMesh解决方案 istio(1):ServiceMesh解决方案-k8s安装istio istio(2):流量管理-基于不同版本访问规则控制 istio(3):流量管理-基 ...

  7. k8s系列---存储卷pv/pvc。configMap/secert

    因为pod是有生命周期的,pod一重启,里面的数据就没了.所以我们需要数据持久化存储. 在k8s中,存储卷不属于容器,而是属于pod.也就是说同一个pod中的容器可以共享一个存储卷. 存储卷可以是宿主 ...

  8. Mysql 5.7 主从复制的多线程复制配置方式

    数据库复制的主要性能问题就是数据延时 为了优化复制性能,Mysql 5.6 引入了 “多线程复制” 这个新功能 但 5.6 中的每个线程只能处理一个数据库,所以如果只有一个数据库,或者绝大多数写操作都 ...

  9. drf序列化高级、自定义只读只写、序列化覆盖字段、二次封装Response、数据库查询优化(断关联)、十大接口、视图家族

    目录 自定义只读 自定义只写 序列化覆盖字段 二次封装Response 数据库关系分析 断外键关联关系 ORM操作外键关系 ORM四种关联关系 基表 系列化类其他配置(了解) 十大接口 BaseSer ...

  10. 面试官:你说你熟悉jvm?那你讲一下并发的可达性分析

    这是why技术的第35篇原创文章 上面这张图是我还是北漂的时候,在鼓楼附近的胡同里面拍的. 那天刚刚下完雨,路过这个地方的时候,一瞬间就被这五颜六色的门板和自行车给吸引了,于是拍下了这张图片.看到这张 ...