Pass inputs to nested component in Angular 2 Pass inputs to nested component in Angular 2 angularjs angularjs

Pass inputs to nested component in Angular 2


I'm not sure if I got it right but here is my implementation ( PLUNKER )


const FIRST_PARTY_OWN_INPUTS = ['not', 'passthrough'];const FIRST_PARTY_PASSTHROUGH_INPUTS = ['all', 'attrs', 'are', 'passed'];const generateAttributes(arr) {   return arr.map(att => '[' + att + '] = "' + att + '"').join(' ');}//-------------------------------------------------------//////////////////import {Component} from '@angular/core'@Component({  selector: 'third-party',  inputs: [...FIRST_PARTY_PASSTHROUGH_INPUTS],  template: `<div>  {{all}} , {{attrs}} ,  {{are}} ,  {{passed}}</div>  `})export class ThirdParty {}@Component({  selector: 'first-party',  inputs: [...FIRST_PARTY_OWN_INPUTS, ...FIRST_PARTY_PASSTHROUGH_INPUTS],  template: `<div>  <div>    {{not}} , {{passthrough}}  </div>  <third-party ${generateAttributes(FIRST_PARTY_PASSTHROUGH_INPUTS)}></third-party>  <first-party-extra></first-party-extra></div>  `,  directives: [ThirdParty]})export class FirstParty {}@Component({  selector: 'my-app',  providers: [],  template: `    <div>      <h2>Hello {{name}}</h2>      <first-party [not]="'not'" [passthrough]="'passthrough'"                     [all]="'all'" [attrs]="'attrs'" [are]="'are'" [passed]="'passed'">      </first-party>    </div>  `,  directives: [FirstParty]})export class App {  constructor() {    this.name = 'Angular2 (Release Candidate!)'  }}

Hope it helps :)


I think this can be boiled down to a more basic problem without Angular2 at all. When you have a function that takes a lot of parameters, it's annoying and error-prone to have to specify all those parameters every time you want to use it. The problem gets worse when there's an intermediate function that doesn't care about those parameters at all - you find yourself adding parameters to the intermediate function just so it can pass it to the inner function. Yeargh!

There are a few patterns for dealing with this. My favorite is to fully instantiate the inner function and pass the instance already loaded up with the former pass-through parameters embedded in it. I think http://blog.mgechev.com/2016/01/23/angular2-viewchildren-contentchildren-difference-viewproviders/ is a nice post about how to do that in Angular 2 using @ViewChild and @ContentChild. Another strategy is to wrap all of the pass-through parameters up in a single object, so at least there's only one parameter to pass through. That also helps when you want to add more parameters - since they're already being wrapped up and passed through opaquely, your pass-through code doesn't need to change.


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: `    <div>      <h2>I am the parent.</h2>      My name is {{firstName}} {{lastName}}.        <my-child firstName="{{firstName}}"                   lastName="{{lastName}}">        </my-child>    </div>  `})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: `    <div>      <h3>I am the child.</h3>      My name is {{firstName}} {{lastName}} Jr.      <br/>     The name I got from my parent was: {{firstName}} {{lastName}}    </div>  `})export class ChildComponent {  @Input() firstName: string;  @Input() lastName: string;}

App Component:

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