Angular2:通过引用传递组件之间的交互 [英] Angular2: Pass by reference to interact between components

查看:94
本文介绍了Angular2:通过引用传递组件之间的交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们将model传递给子组件并对其进行修改时,这些值仅反映在子组件的局部变量中,而对父组件不可用.我们可以通过引用将值从父级传递到子级吗?因此更改在那里也可见.

When we pass a modelto the child component and it modifies it, the values are just reflected in the child components' local variable and not available to the parent. Can we pass values by reference from parent to child. So the changes are visible there as well.

我已经在服务层使用observable实现了相同的功能.但是我们不能通过引用传递@Input吗?

I have implemented the same using an observable at the service layer. But can't we pass by reference through @Input?

推荐答案

原始值(stringnumboolean,对象引用)通过值(复制)传递,对象和数组通过引用传递(两个组件都引用了相同的对象实例).

Primitive values (string, num, boolean, object references) are passed by value (copied), objects and arrays are passed by reference (both components get a reference to the same object instance).

只需将原始值包装在对象中,更改就会在两面反映出来.

Just wrap your primitive values in objects and changes will be reflected on both sides.

Angular2更改检测不会检测到数组或对象属性中值的更改(绑定表达式对它们进行寻址时除外).

Angular2 change detection won't detect changes to values in arrays or object properties (except when binding expressions address them).

这篇关于Angular2:通过引用传递组件之间的交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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