在contentChildren及其“父级”之间进行通信的首选方法是什么?
更新:儿童可能不是直接父母......
给出一个随机的儿童名单,由一群人包裹:
<child-group>
<div>Some other content, child may not be direct parent...</div>
<child *ngFor="let item of data$ | async;"></child>
</child-group>
子组件:
@Component({
selector: 'child',
template: '<button (click)="didSomethingTellGroup()"></button>',
styleUrls: ['child.component.scss'],
})
export class ChildComponent implements OnInit {
constructor() {
}
ngOnInit() {
}
doSomething() {
console.log('Called from group component')
}
didSomethingTellGroup() {
//EventEmitter?
//Observable?
}
}
父组件:
@Component({
selector: 'child-group',
template: '<ng-content></ng-content>',
styleUrls: ['child-group.component.scss'],
})
export class ChildGroupComponent implements AfterContentInit {
@ContentChildren(ChildComponent) childrenList: QueryList<ChildComponent>;
constructor() {
}
ngAfterContentInit() {
//How to invoke doSomething() on all children?
childrenList...
//How can I get notification from one or all children, that it did something, or its state has changed.
childrenList...
}
}
如何从ChildGroup调用子方法?一个孩子如何将信息发送回ChildGroup?
更新
在下面的评论中,我提到当我试图在孩子身上调用一种方法时,什么也没发生。事实证明,我需要订阅更改并等待孩子......然后我就可以调用
了ngAfterContentInit()
{
this.childrenList.changes
.subscribe(list => {
list.forEach(c => c.doSomething())
}
);
}
答案 0 :(得分:3)
对于您的孩子到父母的场景,@ Output事件发射器是您最好的选择。您的子组件正在将内部操作本身(用户单击某处)转换为与该组件的任何用户可以侦听的业务相关事件。
至于父母打电话的孩子,你已经大部分时间用你的榜样了。只需遍历QueryList并调用您希望在子组件上使用的任何方法。
ngAfterContentInit() {
//How to invoke doSomething() on all children?
this.childrenList.forEach ( c => c.doSomething(); )
...
}
答案 1 :(得分:3)
之前的答案是完全正确的,使用@Output
并循环浏览QueryList
是可行的方法,但如果您提到的孩子不是直接的,那么您可以使用服务作为沟通香奈儿。
这是一个非常基本的掠夺者,证明了这一点:http://plnkr.co/edit/VuYiz7gVB42PEnnk2d8C(我不是观察者的专家所以也许这可以改进)。
基本上,当您单击子组件的按钮时,它使用:
this.myService.sendToParent(this.random);
通过服务向父母发送消息。在此函数中,服务通过observable向父节点发送消息:
this.parentSubject.next(value);
以前,父母订阅了这个observable:
this.service.getParentMessages().subscribe((data) =>
{
alert('Something changed from a children: ' + data);
this.service.sendToChildren(data);
});
正如您所看到的,当它收到一个新值时,它使用函数sendToChildren
通过服务向他的所有孩子发送一条消息(同样的原则适用于此)。当孩子收到消息时,它会更改最终显示在组件中的值。