简体   繁体   English

Angular 2使用父组件将Model类传递给我的子组件

[英]Angular 2 passing Model class to my child component using parent component

I have been exploring angular 2 from some time and now am running out while implementing this thing. 我从某个时候开始就一直在探索角度2,现在在实现这个东西时已经精疲力尽了。 I have a child component 我有一个子组件

@Component({
   selector: 'child'
})
class child<T> {}

Now i want my parent class to pass generic type for my child component dynamically using selector or something in parent. 现在,我希望我的父类使用选择器或父类中的某些东西动态地为我的子组件传递通用类型。

@Component({
   selector: 'parent',
   template: `<child></child>`
})
class parent {}

Please let me know if this can be achieved or not. 请让我知道是否可以实现。 Sorry if am asking silly question 不好意思问傻问题

Thank you 谢谢

Define the constructor in the child class that takes an argument of some generic type, eg ProductService. 在子类中定义构造函数,该构造函数接受某种通用类型的参数,例如ProductService。

In your parent component define a provider for ProductService using a factory function, which will check some flag variable and will pass to the child a corresponding concrete implementation of the ProductService. 在您的父组件中,使用工厂函数定义ProductService的提供程序,该函数将检查一些标志变量,并将相应的ProductService具体实现传递给子级。

You can read about using factories here: https://angular.io/docs/ts/latest/guide/dependency-injection.html 您可以在这里阅读有关使用工厂的信息: https : //angular.io/docs/ts/latest/guide/dependency-injection.html

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM