在Angular中的同级组件之间传递数据 [英] Passing Data between sibling components in Angular

查看:59
本文介绍了在Angular中的同级组件之间传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上图描述了我的Angular 2应用程序,主要组件有两个孩子组件"FromComponent"和"ToComponent".

Above image depicts my Angular 2 application , Main component has two child components "FromComponent" and "ToComponent".

"FromComponent"显示项目列表,每个项目旁边都有一个复选框.

"FromComponent" displays a list of items with a checkbox beside each item.

当用户在FromComponent中选择/取消选择一个项目时,应将其从ToComponent的项目列表中添加/删除.

When user selects/deselects an item in FromComponent , it should be added/removed from ToComponent list of items.

在角度2中实现此效果的最佳方法是什么?

what is the best way to implement this in angular 2 ?

推荐答案

您将要使用存储此信息的共享服务.使用observables作为存储机制将允许To组件订阅该主题,并监视From组件对该observable所做的任何更改.

You would want to use a shared service that stores this information. Using observables as the storage mechanism would allow the To Component to subscribe to that subject and watch for any changes that the From component makes to that observable.

在rxjs中查看有关Subject和BehaviorSubject类型的更多信息.

Check out more information on Subject and BehaviorSubject types in rxjs.

这篇关于在Angular中的同级组件之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆