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

前端 未结 3 981

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

@Component({
    selector: \'fixed\',
    template: \'
{{value}}
\' }) e
3条回答
  •  臣服心动
    2020-12-16 12:39

    It seems you can use generic type parameters in Angular 2 child components.

    @Component({
      selector: 'app-child',
      template: '

    Generic Child

    {{cp}}

    ' }) export class GenericChildComponent { @Input() cp: T; } import { GenericChildComponent } from './generic-child.component'; @Component({ selector: 'app-root', template: '
    ', directives: [GenericChildComponent] }) export class ParentComponent { p1: string = 'property 1'; p2: number = 100; }

    This works without recourse either to the ViewChild technique suggested above or to a base class technique suggested here.

提交回复
热议问题