简体   繁体   English

如何在angular2中将对象从父组件传递到子组件?

[英]How to pass an object from parent component to children components in angular2?

Say i have 3 Angular components and first component uses the second and third component as a directive. 说我有3个Angular组件,第一个组件使用第二个和第三个组件作为指令。 They should share the same model object, which is initialized in the first component. 他们应该共享相同的模型对象,该对象在第一个组件中初始化。 How can I pass that model to the second and third component? 如何将该模型传递给第二和第三部分? I referred this post How to pass object from one component to another in Angular 2? 我提到了这篇文章如何在Angular 2中将对象从一个组件传递到另一个组件? but it is using inputs... I want to know all the possible alternatives to share the model object between various child components Somebody please tell me the alternatives which i can follow 但是它正在使用输入...我想知道所有可能的替代方案,以在各个子组件之间共享模型对象。有人请告诉我可以遵循的替代方案

Add the type to the providers list of the First component and inject it into the constructor of First , the involved child, and grandchild constuctors. 将类型添加到First组件的提供程序列表中,并将其注入到First ,涉及的子代和孙代子构造函数的构造函数中。

@Component({
  ...
  providers: [SharedService]
})
export class First {
  constructor(private shared: SharedService);
  // also add this parameter to the other child and grand-child 
  // component and directives constructors and they will get
  // passed a shared instance.
}

Don't add SharedService to any of those child or grand-childrens providers or they'll get a new instance instead of the shared instance. 不要将SharedService添加到任何这些子级或孙级providers否则他们将获得一个新实例而不是共享实例。

If you add SharedService to bootstrap(AppComponent, [SharedService]) instead of the parent component, the whole application shares the seam SharedService instance instead of just a selected subtree. 如果将SharedService而不是父组件添加到bootstrap(AppComponent, [SharedService]) ,则整个应用程序将共享接缝SharedService实例,而不仅仅是选定的子树。

An advantage of using input properties on the children (rather than a shared service) is that Angular change detection will automatically propagate changes to the children. 在子级(而不是共享服务)上使用输入属性的一个优点是,角度更改检测将自动将更改传播到子级。 This is especially useful if you are using immutable objects and you want to change your entire model (to a new instance/reference). 如果您使用不可变的对象,并且想要更改整个模型(到新的实例/引用),则此功能特别有用。

With a service, you can change the model, but not the model reference. 使用服务,您可以更改模型,但不能更改模型参考。

With input properties, you can do either: change the model and/or change the model reference. 使用输入属性,您可以执行以下任一操作:更改模型和/或更改模型参考。

See the following Savkin blog post for more about modeling using immutable data: http://victorsavkin.com/post/133936129316/angular-immutability-and-encapsulation 有关使用不可变数据进行建模的更多信息,请参见以下Savkin博客文章: http ://victorsavkin.com/post/133936129316/angular-immutability-and-encapsulation

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 具有来自父组件的属性的 Angular 子组件 - Angular children components with attributes from parent component 如何从Angular2父组件中的组件列表中调用子组件中的方法? - How to call a method in child component from a list of components in parent component in Angular2? 从父组件角度获取所有子组件的状态 - Angular get status of all children components from parent component 如何在angular2中将变量从子组件模板传递到父组件? - How to pass a variable from a child component template to a parent component in angular2? Angular2 子组件 - Angular2 children components 如何将信息从一个组件传递到 Angular 2+ 中的嵌套子组件? - How to pass information from one component to nested children components in Angular 2+? 如何从angular2中的子组件更新父组件 - how to update parent component from child component in angular2 Angular 2中的RouteData可以将变量传递给来自父组件的路由组件吗? - Can RouteData in Angular 2 pass variables to routed components from parent component? 如何从Angular2的其他组件中调用组件中的函数? - How to call functions in a component from other components in Angular2? Angular2将存储在变量中的数据从app.component.ts(父组件)传输到其他组件 - Angular2 carrying data stored in a variable from app.component.ts(parent component) to other components
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM