2017-08-29 66 views
0

我需要幫助,關於如何在* ngFor循環或任何其他建議中傳遞函數?Angular 2 - 如何通過* ngFor函數

我有待辦事項列表組件和一個孩子的todo項組成部分 - 我想通過這將返回一個值 - 功能請查看下面的代碼:

- 待辦事項列表組件

`<div class="list-group"> 
    <app-todo-item 
    *ngFor="let todo of todos; let i = index" 
    [todoItem]="todo" 
    [index]="i"></app-todo-item> 
</div>` 

- 藤項目組件 `

<a 
    [routerLink]="index" 
    [routerLinkActive]="'list-group-item-warning'" 
    class="list-group-item"> 
    {{ todo.name }} 
    <span class="badge badge-pill" 
    [ngClass]="{'badge-success': todo.status === 'active', 'badge-warning': todo.status === 'inactive'}">{{ todo.status }}</span> 
    <div class="option-group float-right"> 
    <span class="badge badge-dark">2 days ago myFunction(date)</span> 
    </div> 
</a> 
` 

請讓我知道什麼是可能的方式,我可以做到這一點。 在此先感謝

回答

0

您是否試圖將該功能傳遞給應用程序項目組件?

如果是的話,你可以使用@Output從角核心

// import it inside your component 
import { Output, EventEmitter } from '@angular/core' 

// declare the function callback you want to recieve 
@Output() callback = new EventEmitter(); 

// to make the callback send data back call, if you want to pass multiple fields put it all inside an object 
this.callback.emit(datatobepassed) 

// to make use of it do; you need to put $event or else function recieves undefined 
<app-todo-item (callback)=」yourfunction($event)」> 

// in your parent component 
yourfunction(data: any) 
{ 
    // stuff 
} 

希望幫助

+0

感謝我得到了點.. –

+0

一件事,如果我想打印待辦事項模板怎麼我的回調函數值可以做到這一點? –

+0

是否意味着回調函數將數據發送回'app-todo-item'組件? – masterpreenz