Pass inputs to nested component in Angular 2

后端 未结 3 453
难免孤独
难免孤独 2021-02-03 21:42

How can the attributes be transparently translated from wrapper component to nested component?

Considering that there is

const FIRST_PARTY_OWN_INPUTS = [         


        
3条回答
  •  礼貌的吻别
    2021-02-03 21:55

    You can accomplish this by using @Input() on your child components.

    http://plnkr.co/edit/9iyEsnyEPZ4hBmf2E0ri?p=preview

    Parent Component:

    import {Component} from '@angular/core';
    import {ChildComponent} from './child.component';
    
    @Component({
      selector: 'my-parent',
      directives: [ChildComponent],
      template: `
        

    I am the parent.

    My name is {{firstName}} {{lastName}}.
    ` }) export class ParentComponent { public firstName:string; public lastName: string; constructor() { this.firstName = 'Bob'; this.lastName = 'Smith'; } }

    Child Component:

    import {Component, Input} from '@angular/core';
    
    @Component({
      selector: 'my-child',
      template: `
        

    I am the child.

    My name is {{firstName}} {{lastName}} Jr.
    The name I got from my parent was: {{firstName}} {{lastName}}
    ` }) export class ChildComponent { @Input() firstName: string; @Input() lastName: string; }

    App Component:

    //our root app component
    import {Component} from '@angular/core';
    import {ParentComponent} from './parent.component';
    
    @Component({
      selector: 'my-app',
      directives: [ParentComponent],
      template: `
        
    ` }) export class App { constructor() { } }

提交回复
热议问题