Ответ 1
Я думаю, что этот документ может быть вам полезен:
Фактически вы можете использовать наблюдаемый/предмет, который родитель предоставляет своим детям. Что-то вроде этого:
@Component({
(...)
template: `
<child [parentSubject]="parentSubject"></child>
`,
directives: [ ChildComponent ]
})
export class ParentComponent {
parentSubject:Subject<any> = new Subject();
notifyChildren() {
this.parentSubject.next('some value');
}
}
Детский компонент может просто подписаться на эту тему:
@Component({
(...)
})
export class ChildComponent {
@Input()
parentSubject:Subject<any>;
ngOnInit() {
this.parentSubject.subscribe(event => {
// called when the notifyChildren method is
// called in the parent component
});
}
ngOnDestroy() {
// needed if child gets re-created (eg on some model changes)
// note that subsequent subscriptions on the same subject will fail
// so the parent has to re-create parentSubject on changes
this.parentSubject.unsubscribe();
}
}
В противном случае вы могли бы использовать общую службу, содержащую такой объект, таким же образом...