[Angular 2] Refactoring mutations to enforce immutable data in Angular 2

When a Todo property updates, you still must create a new Array of Todos and assign a new reference. This lesson walks you through refactoring from the current approach to the immutable approach.

 

TodoItemRender.ts:

复制代码
import {Component, Input, ViewEncapsulation, EventEmitter, Output} from 'angular2/core';
@Component({
    selector: 'todo-item-renderer',
    // encapsulation:ViewEncapsulation.Emulated, //Default, (parent style pass )in and no (child style go) out
    // encapsulation:ViewEncapsulation.Native, // no in and no out
    encapsulation:ViewEncapsulation.None, // in and out
    template: `
        <style>
            .completed{
                text-decoration: line-through;
            }
        </style>
        <div>    
            <span [ngClass]="todo.status"
                  [contentEditable]="todo.isEdit">{{todo.title}}</span>
            <button (click)="toggleTodo.emit(todo)">Toggle</button>
            <button (click)="todo.edit()">Edit</button>
        </div>
    `
})

export class TodoItemRenderer{
    @Input() todo;
    @Output() toggleTodo = new EventEmitter();
}
复制代码

Here we use EventEmitter to emit a event called 'toggleTodo'. Parent component (TodoList.ts) will catch the event and update the TodoService's todos array.

And todos array should be a new reference.

 

TodoList.ts:

复制代码
import {Component} from 'angular2/core';
import {TodoService} from './TodoService';
import {TodoItemRenderer} from './TodoItemRenderer';
import {StartedPipe} from './started-pipe';

@Component({
    selector: 'todo-list',
    pipes: [StartedPipe],
    directives: [TodoItemRenderer],
    template: `
        <ul>
            <li *ngFor="#todo of todoService.todos | started">
                <todo-item-renderer [todo]="todo"
                (toggleTodo) = "todoService.toggleTodo($event)" // Contains the todoModule
                ></todo-item-renderer>
            </li>
            
        </ul>
    `
})

export class TodoList{
    constructor(public todoService: TodoService){
        
    }
}
复制代码

 

TodoService.ts:

复制代码
import {TodoModule} from './TodoModule';
export class TodoService {
    todos = [
        //init some todos
        new TodoModule("eat"),
        new TodoModule("sleep"),
        new TodoModule("code")
    ];

    addNewTodo(todo) {
        this.todos = [...this.todos, todo];
    }

    toggleTodo(todo) {

        const i = this.todos.indexOf(todo);

        todo.toggle();

        this.todos = [
            ...this.todos.slice(0, i),
            todo,
            ...this.todos.slice( i + 1)
        ];
    }
}
复制代码

 

---------------------

 

posted @   Zhentiw  阅读(231)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示