如何从父组件发送数据到子组件[Angular 2]

时间:2016-12-23 15:43:05

标签: javascript angular typescript

从父组件我喜欢这样:

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

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

和子组件:

@Input() filteredBanks: Subject<any>;

ngOnInit() {

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

}

我在控制台中收到错误: Cannot read property 'subscribe' of undefined

任何人都知道如何解决这个问题?

修改

父模板:

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

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

1 个答案:

答案 0 :(得分:0)

在您的父模板中,它应该是

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

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

我认为app-singlebank是子组件的选择器