[Angular 2] Passing data to components with @Input

Posted Answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Angular 2] Passing data to components with @Input相关的知识,希望对你有一定的参考价值。

@Input allows you to pass data into your controller and templates through html and defining custom properties. This allows you to easily reuse components, such as item renderers, and have them display different values for each instance of the renderer.

 

For this part of code, we use ‘todo‘ a lot, so it would be a good idea to exract a complete to handle all the action about todo:

        <ul>
            <li *ngFor="#todo of todoService.todos">
                <span [hidden]="todo.status == ‘completed‘"
                    [contentEditable]="todo.isEdit">{{todo.title}}</span>
                <button (click)="todo.toggle()">Toggle</button>
                <button (click)="todo.edit()">Edit</button>
            </li>
            
        </ul>

 

TodoItemRenderer.ts: 

import {Component, Input} from ‘angular2/core‘;
@Component({
    selector: ‘todo-item-renderer‘,
    template: `
        <div>    
            <span [hidden]="todo.status == ‘completed‘"
                  [contentEditable]="todo.isEdit">{{todo.title}}</span>
            <button (click)="todo.toggle()">Toggle</button>
            <button (click)="todo.edit()">Edit</button>
        </div>
    `
})

export class TodoItemRenderer{
    @Input() todo
}

 

TodoList.ts:

import {Component} from ‘angular2/core‘;
import {TodoService} from ‘./TodoService‘;
import {TodoItemRenderer} from ‘./TodoItemRenderer‘;

@Component({
    selector: ‘todo-list‘,
    directives: [TodoItemRenderer],
    template: `
        <ul>
            <li *ngFor="#todo of todoService.todos">
                <todo-item-renderer [todo]="todo"></todo-item-renderer>
            </li>
            
        </ul>
    `
})

export class TodoList{
    constructor(public todoService: TodoService){
        
    }
}

 

以上是关于[Angular 2] Passing data to components with @Input的主要内容,如果未能解决你的问题,请参考以下文章

3D Computer Grapihcs Using OpenGL - 07 Passing Data from Vertex to Fragment Shader

Clustering by Passing Messages Between Data Points(Brendan J.Frey* and Delbert Dueck)

Clustering by Passing Messages Between Data Points(Brendan J.Frey* and Delbert Dueck)例子

使用 .NET 在 Angular 和 Cosmos DB 之间传递“纯”(无类)json

将 'dict' 对象从 Django 模板传递到 Angular 控制器 - 避免 jsonify 和解析

Angular 2 Data Flow 和 Flux 之间的关键区别是啥?