2017-06-15 18 views
0

データを渡す単純な2つのコンポーネントでは、これは簡単です。しかし、私の場合、私はthirdy-partyのdatepickerプラグインを使用しています。私のアプリケーションのさまざまなフォームに日付フィールドがたくさんあるので、私はdatePickerのコンポーネントを作成して、それぞれの設定を繰り返す必要はありません私はそれを使用することを確認します。入れ子になったコンポーネントの角度2のパス値

親-component.ts

... 
template: `<my-custom-datepicker-component [model]="model"></my-custom-datepicker-component> 
<span>{{ model }}</span> 
` 
... 
export class ParentComponent{ 
    model: any; 
} 

と私のカスタム-datepicker.ts中:問題は今、私はまた、このような日付ピッカーコンポーネントに渡されている子コンポーネントにモデルを渡す午前ですhttps://nkalinov.github.io/ng2-datetime/を:

... 
template: '<datetime [(ngModel)]="model"></datetime>' 
... 
export class MyCustomDatepickerComponent{ 
    @Input() model: any; 
} 

そして、ここでは、私が使用しているDatePickerのプラグインです。

私の問題は、選択した日付が親コンポーネントのモデルに反映されないということです。誰かが私を助けることを願っています。前もって感謝します!

+0

あなたは異なる可能性を説明するガイドを読みましたか? [コンポーネントの相互作用ガイド](https://angular.io/guide/component-interaction) – sabithpocker

+0

はい、私はすでにやりました。私は、実際の日付ピッカーのプラグインコンポーネントにカスタム日付ピッカーコンポーネントに私の親コンポーネントから、ここで、ネストされたコンポーネントの3レベルがあるので、このシナリオが例よりも複雑だと思います。 – Jed

+1

3つのコンポーネントのコンポーネントコードを追加すると、それを調べることができます。アイデアは 'EventEmitter'を使って子から親に値を渡すことです。 Banana Braces '[(x)]'を使うこともできますが、カスタムコンポーネントに 'x'の' xChanges'イベントを実装する必要があります。 'parent.component.ts'、'私のカスタム・datepicker.component.ts'コードと、使用している 'datetime'コンポーネントへのリンクを追加します。 – sabithpocker

答えて

-1

は、あなたが私の-カスタムdatepicker.tsで @Inputプロパティモデルをバインドしていますか?

@Input() model: string; 
+0

はい、私は、すべての基本的なものを考慮してくださいでした。 – Jed

0

私のカスタム-datepicker.tsでこの

よう試してみて

@Input()モデル:任意の;

あなたは

+0

はい、私はそれを置いた、基本的なすべてがあると考えてください。 – Jed

+0

コードでプランナーを作成できるか –

1

私-カスタム日付picker.component.ts

template: '<datetime [(ngModel)]="model" 
     (ngModelChange)="dateChanged($event)"></datetime>' 
... 
export class MyCustomDatepickerComponent{ 
    @Input() date: any; 
    @Output() dateChange = new EventEmitter<any>(); 
    // dateChanged will be called when datetime model changes 
    // this will also trigger model 
    dateChanged(date: any){ 
     this.dateChange.emit(date); 
    } 
} 

parent.component.ts

コンポーネントにthis.model使用してモデルにアクセスすることができます
... 
template: `<my-custom-datepicker-component 
      [(date)]="programDate"></my-custom-datepicker-component> 
<span>{{ programDate }}</span> 
` 
... 
export class ParentComponent{ 
    programDate: any; 
} 

  1. Child to parent communication with EventEmitter.
  2. Two-way binding with Event Emitter

しかし、あなたはカスタム日付ピッカー言うとき、あなたは実際にカスタムフォーム要素を作成しようとしている:OR

... 
template: `<my-custom-datepicker-component 
      (dateChange)="customComponentDateChanged($event)" [date]="programDate"> 
      </my-custom-datepicker-component> 
<span>{{ programDate }}</span> 
` 
... 
export class ParentComponent{ 
    programDate: any; 
    customComponentDateChanged(date: any) { 
    this.programDate = date; 
    } 
} 

これが唯一の説明します。その場合は、あなたが検証を実装する必要があるとngModel/ngControl/ControlValueAccessorとしてhere

が説明したように、それはあなたが<datetime>にいくつかの新しい動作を追加したい場合は、あなたがすることができ、常にextend、言われていること、もう少し複雑ですoriginal component。それを拡張する無傷でその機能を維持します(@Componentデコレータdoesntの取得が継承されたとして、あなたが新しいテンプレート/ CSSを提供する必要があります)、あなたはあなた自身を追加することができます。

は、なぜあなたは<datetime>の上にカスタム・コンポーネントを作成していますか?

関連する問題