[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){

    }
}
时间: 2024-10-11 10:58:45

[Angular 2] Passing data to components with @Input的相关文章

[Angular 2] Passing data to components with &#39;properties&#39;

Besides @Input(), we can also use properties on the @Component, to pass the data. import {Component, View, NgFor, Input} from 'angular2/angular2'; @Component({ selector: 'reddit-article' }) @View({ directives: [], template: ` <article> <div class

[Angular 2] Passing Observables into Components with Async Pipe

The components inside of your container components can easily accept Observables. You simply define your custom @Input then use the Async pipe when you pass the Observable in. This lesson walks you through the process of passing an Observable into a

WIN7系统 64位出现 Net Framework 数据提供程序要求 Microsoft Data Access Components(MDAC).

WIN7系统 64位出现  Net Framework 数据提供程序要求 Microsoft Data Access Components(MDAC).请安装 Microsoft Data Access Components(MDAC)2.6或更高的版本.怎么解决,已经下载了2.8版本安装了,但是还是不顶用. 2015-12-02 10:51网友采纳 这应该是你安装的系统有精简过系统文件,导致安装一些程序缺乏文件出错.换个系统吧.可到我的系统贴吧下载GHO系统与GHO安装工具,可以在进入现在的系

跨数据存取控件Universal Data Access Components

最近发现MDT推出去的系统的有不同问题,其问题就不说了,主要是策略权限被域继承了.比如我们手动安装的很多东东都是未配置壮态,推的就默认为安全壮态了,今天细找了一下,原来把这个关了就可以了. 跨数据存取控件Universal Data Access Components

问题-Error creating object. Please verify that the Microsoft Data Access Components 2.1(or later) have been properly installed.

问题现象:软件在启动时报如下错误信息:Exception Exception in module zhujiangguanjia.exe at 001da37f. Error creating object. Please verify that the Microsoft Data Access Components 2.1(or later) have been properly installed. 问题原因:是启动程序时访问ACCESS库时没有WIN的组件或是版本过低. 解决方法:查看组

[Angular 2] Passing Template Input Values to Reducers

Angular 2 allows you to pass values from inputs simply by referencing them in the template and passing them into your Subject.next() call. This lesson shows you how to make a number input and pass the value so you can configure how much you want the

AngularJS - Passing data between pages

You need to create a service to be able to share data between controllers. app.factory('myService', function() { var savedData = {} function set(data) { savedData = data; } function get() { return savedData; } return { set: set, get: get } }); In you

[React Native] Passing data when changing routes

The way you make HTTP requests in React Native is with the Fetch API. In this video we'll talk about Fetch and how to work with promises. As we build application components, we will need to pass data along as we change routes and bring them into view

[Angular2 Router] Use Params from Angular 2 Routes Inside of Components

Angular 2’s ActivatedRoute allows you to get the details of the current route into your components. Params on the ActivatedRoute are provided as streams, so you can easily map the param you want off of the stream and display it in your template. For