入力の値を親コンポーネントに渡したいとします。現在、私は子コンポーネントから入力全体をElementRef
に送信しています。これを行うためのエレガントな方法はありますか?つまり、参照番号全体ではなく、1つの番号だけを送る必要があります。入力値を親コンポーネントに渡す方法
子コンポーネント:
import { Component, ViewChild } from '@angular/core';
@Component({
selector: 'app-action-dialog-content',
template: `
<md-input-container>
<input #amount md-input placeholder="Amount">
<span md-suffix>€</span>
</md-input-container>
`
})
export class ActionDialogContentComponent {
@ViewChild('amount') amount;
}
親コンポーネント:
import { Component, ViewChild } from '@angular/core';
import { ActionDialogContentComponent } from './../action-dialog-content/action-dialog-content.component';
@Component({
selector: 'app-action-dialog',
template: `
<app-action-dialog-content></app-action-dialog-content>
<md-dialog-actions>
<button md-raised-button (click)="sendData()">ADD</button>
</md-dialog-actions>
`
})
export class ActionDialogComponent {
@ViewChild(ActionDialogContentComponent) amountInput: ActionDialogContentComponent;
sendData() {
console.log(this.amountInput.amount.nativeElement.value);
}
}
あなたがチェック詳細については 'EventEmitter'を使用することができ、このHTTPS:/ /angular.io/docs/ts/latest/cookbook /component-communication.html#!#child-to-parent – rashfmnb