简体   繁体   中英

How to emit data from parent to child component [Angular 2]

From parent component I do like this:

import {Observable, Subscription, Subject} from 'rxjs';

filteredBanks: Subject<any> = new Subject();
cilickFun() {
 this.filteredBanks.next('some value');
}

And child component:

@Input() filteredBanks: Subject<any>;

ngOnInit() {

        this.filteredBanks.subscribe(event => {
            console.log(event);
        });

}

I get error in console: Cannot read property 'subscribe' of undefined

Anyone know how to solve this?

EDIT

parent template:

<a (click)="cilickFun()"> EMIT</a>

<div class="col-md-9">
      <app-singlebank></app-singlebank>
</div> 

In your parent template it should be

<a (click)="cilickFun()"> EMIT</a>

<div class="col-md-9">
      <app-singlebank [filteredBanks]='filteredBanks'></app-singlebank>
</div> 

I assume that app-singlebank is the selector for child component

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM