todoMVC_mvc框架是什么

todoMVC_mvc框架是什么依赖cssnpmitodomvc-commontodomvc-app-cssapp.component.tsimport{Component}from’@angular/core’;consttodos=[{id:1,title:’吃饭’,done:true},{id:1,title:’工作’,done:false},{id:1,title:’运动’,

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

依赖css

npm i todomvc-common todomvc-app-css

app.component.ts

import { 
   Component} from '@angular/core';


const todos = [
  { 
   
    id: 1,
    title: '吃饭',
    done: true
  },
  { 
   
    id: 1,
    title: '工作',
    done: false
  },
  { 
   
    id: 1,
    title: '运动',
    done: true
  }
]


@Component({ 
   
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent { 
   
  h_title = 'todo-angular';
  public todos: { 
   
    id: number,
    title: string,
    done: boolean
  }[] = JSON.parse( window.localStorage.getItem('todos') || '[]');
  // 该函数是一个特殊的angular生命周期钩子函数
  // 它会在angular应用初始话的时候执行一次
  ngOnInit() { 
   
    window.onhashchange = () => { 
   
      this.hashChangeHandler()
      // 当用户点击了锚点的时候,我们需要获取当前的锚点标识
      // 然后动态的将根组件visibility设置为当前点击的锚点标识
      //注意 bind ,不然的话this就变成window了
      window.onhashchange = this.hashChangeHandler.bind(this)
    }
  }

  // 当angular组件数据发生改变的时候,ngDoCheck钩子被触发
  // 在钩子函数中持久化数据
  ngDoCheck(){ 
   
    window.localStorage.setItem('todos', JSON.stringify(this.todos))
  }
  public currentEditing: { 
   
    id: number,
    title: string,
    done: boolean
  } = null
// 实现导航切换数据过滤
// 1. 提供一个属性,该属性会根据当前点击的连接返回过滤后的数据 filterTodos
// 2. 提供一个属性,用来存储当前点击的连接标识 visibility all active completed
// 3. 为连接提供点击事件,当点击导航链接的时候,改变
//
  public visibility: string = 'all'

  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)
    }
    return null
  }

  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) { 
   
    this.todos.forEach(t => t.done = val)
  }

  removeTodo(index: number) { 
   
    this.todos.splice(index, 1)
  }

  saveEdit(todo, e) { 
   
    this.currentEditing = null
    todo.title = e.target.value
  }

  handleEditUp(e) { 
   
    const { 
   keyCode, target} = e
    if (keyCode === 27) { 
   
      target.value = this.currentEditing.title
      this.currentEditing = null
    }
  }

  get remainingCount() { 
   
    return this.todos.filter(t => !t.done).length
  }

  hashChangeHandler(){ 
   
    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" ng-app="">
  <header class="header">
    <h1>待办事项</h1>
    <input class="new-todo" placeholder="What needs to be done?" autofocus (keyup.enter)="addTodo($event)" >
  </header>
  <!-- This section should be hidden by default and shown when there are todos -->
  <section class="main" *ngIf="todos.length">
    <input id="toggle-all" class="toggle-all" type="checkbox" [checked]="toggleAll">
    <!-- (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 *ngFor="let todo of filterTodos; let i = index;" [ngClass]="{ editing: currentEditing === todo, completed: todo.done }">
        <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.enter)="saveEdit(todo, $event)" (keyup)="handleEditUp($event)" (blur)="saveEdit(todo, $event)">
      </li>
    </ul>
  </section>
  <!-- This footer should be hidden by default and shown when there are todos -->
  <footer class="footer" *ngIf="todos.length">
    <!-- This should be `0 items left` by default -->
    <span class="todo-count"><strong>{
  
  {remainingCount}}</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>
</section>

style.css

/* You can add global styles to this file, and also import other style files */
@import url('~todomvc-common/base.css');
@import url('~todomvc-app-css/index.css');

tsconfig.json

"strict": false,

app.module.ts

import { 
    NgModule } from '@angular/core';
import { 
    BrowserModule } from '@angular/platform-browser';
import { 
   FormsModule} from "@angular/forms";
import { 
    AppComponent } from './app.component';

@NgModule({ 
   
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    FormsModule
  ],
  providers: [],
  bootstrap: [AppComponent]
}) export class AppModule { 
    }

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/194860.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 关于WinExec和System的比较

    关于WinExec和System的比较http://nt04.blog.163.com/blog/static/3297804920116246622829/WinExec是一个WIN32API,它的第一个参数必须包含一个可执行文件名,SYSTEM是C库函数,它接受一个DOS命令。你可以这样:WinExec(“command/CdirC:\>dir.txt”,SW_HIDE);system

    2022年7月27日
    5
  • ArcGIS转换坐标系_不同坐标系之间的转换

    ArcGIS转换坐标系_不同坐标系之间的转换ArcGIS 基础8-坐标系转换

    2022年4月20日
    69
  • AIC准则的理解

    AIC准则的理解本文介绍AIC准则的产生及应用。

    2022年5月10日
    80
  • vhdl与verilog hdl的区别_HDL语言

    vhdl与verilog hdl的区别_HDL语言HDL特别是VerilogHDL得到在第一线工作的设计工程师的特别青睐,不仅因为HDL与C语言很相似,学习和掌握它并不困难,更重要的是它在复杂的SOC的设计上所显示的非凡性能和可扩展能力。 在学习HDL语言时,笔者认为先学习VerilogHDL比较好:一是容易入门;二是接受VerilogHDL代码做后端芯片的集成电路厂家比较多,现成的硬核、固核和软核比较多。小析VHDL与Veril

    2022年9月21日
    0
  • Handler、HandlerThread理解

    Handler、HandlerThread理解Handler在android线程编程中非常常见。线程中的handler使用原理:每个线程只有一个Looper来管理消息队列,handler在使用的时候需要绑定到对应的Looper上。Handler给自己绑定的Looper不断的发送消息,Looper来做死循环来不断读取MessageQueue队列中的消息,发送给handler来进行处理。 Android的UI是运行在主线程中,主线程是用MainL…

    2022年7月14日
    17
  • wing是什么_计算二叉树的深度和叶子结点数

    wing是什么_计算二叉树的深度和叶子结点数设一个 n 个节点的二叉树 tree 的中序遍历为(1,2,3,…,n),其中数字 1,2,3,…,n 为节点编号。每个节点都有一个分数(均为正整数),记第 i 个节点的分数为 di,tree 及它的每个子树都有一个加分,任一棵子树 subtree(也包含 tree 本身)的加分计算方法如下:subtree的左子树的加分 × subtree的右子树的加分 + subtree的根的分数若某个子树为空,规定其加分为 1。叶子的加分就是叶节点本身的分数,不考虑它的空子树。试求一棵符合中序遍历为(1,2,

    2022年8月9日
    6

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号