问题描述
我有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中将对象从一个组件传递到另一个组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!