[英]Angular 2+ content projection share data between components
i want to pass some data to the nested component from parent component, can anybody help me with this, thank you我想将一些数据从父组件传递给嵌套组件,有人可以帮我吗,谢谢
<parent> <nested-comp></nested-comp> </parent>
having有
@Component()
class NestedComponent{
@Input("someInput")
private yourDataInput:any
}
<parent> <nested-comp [someInput]="someData"></nested-comp> </parent>
will allow you to pass data between those 2 components by setting binding someData
into yourDataInput
field将允许您通过将绑定
someData
设置为yourDataInput
字段在这两个组件之间传递数据
You need to use @Input() in Angular, for example:您需要在 Angular 中使用@Input(),例如:
In parente component html you set a label or anything you need to pass to child component:在父组件 html 中,您设置 label 或您需要传递给子组件的任何内容:
private label: string = 'Label Test';
In parent component html you set the parameter to nested component:在父组件 html 中,您将参数设置为嵌套组件:
<app-child [label]="label"></app-child>
In nested component ts you declare:在嵌套组件 ts 中声明:
@Input() label: string;
In nested component html you use:在嵌套组件 html 中,您使用:
<label>{{label}}</label>
Doing this you will be able to pass any data from your parent to child components in Angular.这样做您将能够将任何数据从您的父组件传递到 Angular 中的子组件。
You can try to do that with @ViewChild.您可以尝试使用@ViewChild 来做到这一点。 Also RxJS Subject, BehaviourSubject can be used for passing data between components(mostly for independent components)
RxJS Subject, BehaviourSubject 也可用于组件之间的数据传递(主要用于独立组件)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.