2016-02-02 32 views
0

Angular 2 - 我需要一個組件來輸出一個函數,以便嵌套的組件可以調用它。Angular 2 - 我需要一個組件來輸出一個函數,以便嵌套組件可以調用它。

我使用打字稿和角2.

這是輸出代碼:

@Component({ 
    selector: 'jobs', 
    providers: [Job, JobService, Notification, NotificationService, Counties, Towns], 
    templateUrl: './app/components/job/jobs.html', 
    directives: [CORE_DIRECTIVES, FORM_DIRECTIVES, NgIf, NgFor, DialogJob, BootFlatDatePicker], 
    pipes:[SearchFilter], 


    outputs: ['jobCancelledNotification'] 


}) 

jobCancelledNotification = (event) => {console.log("job cancelled via output!!!!"); } 

我需要「jobCancelledNotification」被從嵌套對話框組件調用。

對於我的嵌套的對話框組件我有一個輸出:

@Output() jobCancelledNotification = new EventEmitter(); 

然後我嘗試用這個稱呼它:

this.jobCancelledNotification.emit("event"); 

當對話框組件準備是需要調用jobCancelledNotification這就要求jobCancelledNotification來自父組件。

+0

什麼也沒有發生,我不甚至得到一個控制檯錯誤或編譯器錯誤 – AngularM

回答

1

首先,您不需要包含它們已包含的NgIf,NgFor和CORE_DIRECTIVES。

在父組件:

```<message (message)="call($event)"></message>``` 
export class App { 
response: any; 
call(message: any) { 
    this.response = message; 
} 
} 

在子組件:

@Output() message = new EventEmitter(); 
constructor() { 
setTimeout(()=> { 
    this.message.emit("Hello from MessageCmp"); 
},2000); 
} 

https://plnkr.co/edit/vUocS8dDTKsJFGGSSKYk?p=preview

+0

當我發出我可以打電話從另一個組件發射? – AngularM

+0

所有我想實現的是嵌套組件在其父組件中調用一個方法 – AngularM

+0

我改進了我的答案,希望它會有所幫助。 –

相關問題