2017-04-20 21 views
1

https://angular.io/docs/ts/latest/cookbook/component-communication.html#!#parent-to-view-child調用子組件的方法,如果父母有相同的成分

所以要根據「官方」文檔的兩個孩子,子組件的調用方法可以父類中使用來完成:

@ViewChild(CountdownTimerComponent) 
private timerComponent: CountdownTimerComponent; 

,做

timerComponent.methodName() 

那麼,如果父組件使用兩個CountDownTimerComponent,只想要調用timerComponentNumber1.methodName()?

假設開發人員要調用父類中的方法,而不是從模板

回答

1

您需要使用@ViewChildren代替

@ViewChildren(CountdownTimerComponent) 
private timerComponents: QueryList<CountdownTimerComponent>; 

您可以遍歷timerComponents並調用需要的方法;或做這樣的事情:

this.timerComponents.toArray()[0].someMethod(); 

下面是引用the documentation

+0

好的,等等,讓我試試看 – anaval

相關問題