Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pass a generic type parameter to an Angular2 component?

Let's say I got a component with a fixed input parameter type,

@Component({     selector: 'fixed',     template: '<div>{{value}}</div>' }) export class FixedComponent {     @Input() value: string; } 

How do I go about making that parameter type generic, i.e.

@Component({     selector: 'generic',     template: '<div>{{value}}</div>' }) export class GenericComponent<T> {     @Input() value: T; } 

That is, how do I pass the type in the template of the parent component?

<generic ...></generic> 
like image 634
Thorsten Westheider Avatar asked May 15 '16 06:05

Thorsten Westheider


People also ask

How to communicate between parent and child components in Angular?

@Input() and @Output() give a child component a way to communicate with its parent component. @Input() lets a parent component update data in the child component. Conversely, @Output() lets the child send data to a parent component.

What is generic component in angular?

In TypeScript, generics can be used to better describe the types in classes that are reusable with multiple derived types. Angular does not support instantiation of generic components, but this limitation can be worked around by creating a derived component for each derived type we want to use it with.


1 Answers

It seems that when using AOT compilation the only way to do this is to replace the generic type with 'any'. See https://github.com/angular/angular/issues/11057

like image 180
Aaron S Avatar answered Nov 10 '22 06:11

Aaron S