2017-04-07 24 views
2

私は2つのコンポーネントFormComponentTest1Componentを持っています。なぜContentChildが定義されていないのですか?

Test1ComponentはなぜTest1Component.saveForm()this.contentChildFormをログに記録されているFormComponent

FormComponent.ts

import { Component, Input, Output, EventEmitter } from '@angular/core'; 

@Component({ 
    selector: 'app-form', 
    template:` 
     <div class="panel panel-default fcs-form"> 
     <div class="panel-header form-header"> 
       {{headerTitle}} 
     </div> 

     <div class="panel-body form-body"> 
      <ng-content select="[form-body]"></ng-content> 
     </div> 
     <div class="panel-footer text-center form-footer"> 
       <button class="btn btn-primary">{{resetBtnText}}</button> 
      <button class="btn btn-primary" (click)="saveForm()"> {{saveBtnText}} </button> 
      <button class="btn btn-primary">{{addBtnText}}</button> 
     </div> 
     </div> 
    ` 
}) 
export class FormComponent{ 
    @Input() headerTitle:string = "Header Title"; 
    @Input() saveBtnText:string = "Save"; 
    @Input() resetBtnText:string = "Reset"; 
    @Input() addBtnText:string = "Add"; 
    @Output() save:EventEmitter<any> = new EventEmitter(); 

    constructor(){} 

    saveForm(e: any){ 
    this.save.emit("save"); 
    console.log("FormComponent save"); 
    } 
} 

Test1Component.ts

import { Component, Inject, OnInit, ContentChild } from '@angular/core'; 
import { FormGroup, FormBuilder } from '@angular/forms'; 
import { FormComponent } from './form.component'; 

@Component({ 
    selector: 'app-test1', 
    template: ` 
    <app-form headerTitle="my header" (save)="saveForm(complexForm.value, $event)"> 
     <div form-body> 

      <div class="jumbotron"> 
       <form [formGroup]="complexForm" (ngSubmit)="submitForm(complexForm.value)"> 

        <div class="form-group"> 
         <label>First Name:</label> 
         <input class="form-control" type="text" placeholder="John" [formControl]="complexForm.controls['firstName']"> 
        </div> 

       </form> 
      </div> 

     </div> 
    </app-form> 
    ` 
}) 
export class Test1Component { 
    @ContentChild(FormComponent) contentChildForm: FormComponent; 

    complexForm : FormGroup; 

    constructor(private fb: FormBuilder){ 
    this.complexForm = fb.group({ 
     'firstName' : "", 
     'lastName': "", 
     'gender' : "Female", 
     'hiking' : false, 
     'running' : false, 
     'swimming' : false 
    }); 
    } 

    saveForm(){ 
    console.log(this.contentChildForm); 
    debugger; 
    console.log("Test1Component save"); 
    return true; 
    } 
} 

を表示するためにng-contentを使用していますとしてundefined

修正方法?

これは、私がhttps://plnkr.co/edit/xbTgRuSd7nBIrOWsE1zOを作成したプランカーです。コンソールを開いてログを参照してください。

答えて

3

私はあなたのTest1Component

Modified Plunker

+1

タイプFormComponentを持つ任意の映写のノードが表示されていないので、私は実際に私が正しい答えだと思うあなたのケースで@ViewChildを使用します。これは '@ ViewChild'と' @ContentChild'をいつ使うべきかを知るのに便利です:http://stackoverflow.com/questions/34326745/whats-the-difference-between-viewchild-and-contentchild – SrAxi

+1

@SrAxiそれは物事を作った概念的に明確かつ従いやすい –

+0

@yurzui ViewChild参照を使用して子にデータを渡すことは賢明でしょうか?または入力デコレータを使用しますか? –

関連する問題