Person
のようなクラスオブジェクトを、parent
コンポーネントのchild
コンポーネントに問題なく渡すことができます。しかし、child
コンポーネントでそのオブジェクトを操作して、parent
コンポーネントに戻したいと思います。角(4):クラスオブジェクトを子コンポーネントから親コンポーネントに渡す
これは、子コンポーネントのクラスです:
export class ActionPanelComponent {
@Input('company') company: Company;
@Output() notify: EventEmitter = new EventEmitter();
constructor() {
}
public deleteCompany() {
console.log('display company');
console.log(this.company);
// FIXME: Implement Delete
this.company = new Company();
}
public onChange() {
this.notify.emit(this.company);
}
}
これは、このコンポーネント(抜粋)のhtmlです:これは、親コンポーネントクラス(抜粋)です
<div class="row" (change)="onChange()">
<div class="col-xs-2">
<button md-icon-button >
<md-icon>skip_previous</md-icon>
</button>
</div>
:
public onNotify(company: Company):void {
this.company = company;
}
と親コンポーネントのHTML(抜粋):
<action-panel [company]="company" (notify)="onNotify($event)"></action-panel>
私は.emit
の内側に私のcompany
オブジェクトを渡すことはできませんし、何も動作するので、私は何か間違ったことをしています。
コンポーネント間の双方向オブジェクトバインディングを実現する正しい方法は何ですか? ありがとうございます! EventEmitter
べきemit
をType
TSを伝えるために、このような
子供の操作ではどういう意味ですか?インスタンス属性を設定しますか?そうであれば、変更は参照によって値を渡すときに親に直ちに反映されます。 –
@ Jota.Toledo私は 'company'オブジェクトを子に渡します。子供の場合、私はこの「会社」オブジェクトのすべてのフィールドをクリアしたいです。これはうまくいきません:( –
'EventEmitter'に' Type'を追加するだけです:new EventEmitter(); '。 –
Arg0n