如何在Angular 2中将对象从一个组件传递到另一个组件? [英] How to pass object from one component to another in Angular 2?

查看:29
本文介绍了如何在Angular 2中将对象从一个组件传递到另一个组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Angular 组件,第一个组件使用第二个作为指令.它们应该共享在第一个组件中初始化的相同模型对象.如何将该模型传递给第二个组件?

I have Angular components and first component uses the second one as a directive. They should share the same model object, which is initialized in the first component. How can I pass that model to the second component?

推荐答案

组件2,指令组件可以定义一个输入属性(Typescript中的@input注解).组件 1 可以将该属性从模板传递给指令组件.

Component 2, the directive component can define a input property (@input annotation in Typescript). And Component 1 can pass that property to the directive component from template.

请参阅此 SO 答案 如何在 Angular2 中进行主从组件之间的相互通信?

See this SO answer How to do inter communication between a master and detail component in Angular2?

以及如何将输入传递给子组件.在您的情况下,它是指令性的.

and how input is being passed to child components. In your case it is directive.

这篇关于如何在Angular 2中将对象从一个组件传递到另一个组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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