0
A
回答
0
可以acomplish,使用EventEmitter
和@Output
:
在下面的代碼片段,您可以調用passDataToParent()
函數傳遞所需的數據。
child.component.ts
import { Component, OnInit, Output, EventEmitter } from '@angular/core';
@Component({
selector: 'app-child-component',
templateUrl: './child.component.html',
styleUrls: ['./child.component.css']
})
export class ChildComponent implements OnInit {
// ******** Important part ******
@Output() emitter: EventEmitter<any[]> = new EventEmitter();
dataToEmit : any = "data to pass to parent component";
constructor() {}
ngOnInit() { }
//Call this function to pass data
passDataToParent() {
this.emitter.emit(this.dataToEmit);
}
}
parent.component.ts
import { Component, OnInit, ViewChild } from '@angular/core';
import { ChildComponent } from './child-component';
@Component({
selector: 'app-parent-component',
templateUrl: './parent.component.html',
styleUrls: ['./parent.component.css']
})
export class ParentComponent implements OnInit {
// ******** Get reference of child component ******
@ViewChild(ChildComponent) child : ChildComponent ;
constructor() {}
ngOnInit() { }
receiveDataFromChild(data) {
console.log(data);
}
}
最後在父母HTML
parent.component.html
<app-child (emitter)="receiveDataFromChild($event)"></app-child >
希望它能幫助!
+0
thk you !!但我已經開始閱讀這本書了! ^^ thck爲你提供幫助 –
0
使用eventEmitter。在這裏,我刪除了所選項目的值,但您可以刪除項目。對於異步渲染問題,你可以使用輸出EventEmitter和true。
@Input() items: any[];
@Input() selectedItem: any;
keys: string[] = [];
@Input() sorting: string;
@Output() selectedItemChange = new EventEmitter<any>(true);
相關問題
- 1. 將子組件的輸出發送給父 - 角2 2
- 2. 角2:父子組件屬性綁定
- 3. 角度2訪問父組件的子組件屬性
- 4. 如何從角度2中的子組件中的父組件獲取變量?
- 5. 如何以角度2顯示組件?
- 6. 發送從子組件陣列,以父
- 7. 角2在組件
- 8. 角4父組件事件偵聽器不會被孩子發出的事件
- 9. 如何從主數據發送到子組件角2
- 10. 從父模塊的另一個組件包裝角2組件
- 11. 重置角度2父組件和子組件上的所有輸入
- 12. 從父組件調用角2 ng2-bootstrap模態子組件內部
- 13. 在父組件中使用子組件時出現錯誤 - Angular 2 Unit Testing(KarmaJasmine)
- 14. 角2,可以在組件聲明
- 15. 如何從子組件與可觀察到的在父使用EventEmitter(角2)
- 16. 從子組件導航時更新父組件(角度)
- 17. $發出從小孩到父組件的Vue事件2
- 18. 角2 - 兒童航線有不同的父組件到父組件
- 19. 不能在角2組件
- 20. 角2:在組件類
- 21. 離子/角2組件事件發射器不更新視圖
- 22. 如何在父組件中使用可修改的子組件?
- 23. 如何在Angular2中從父組件類訪問子組件類
- 24. 作出反應如何在父組件
- 25. Angular 2父組件調用多個子組件方法
- 26. Angular 2 - 獲取子組件加載父組件(動態創建)
- 27. Ember - 如何在其子組件上創建父組件觸發器行爲
- 28. Angular 2.1.0父組件未捕獲從子組件發出的事件
- 29. 角2:嵌套子組件靈活
- 30. 角2:ngIf不是一個子組件
你可以找到例如[文件]中(https://stackoverflow.com/documentation/angular/10836/sharing-data-among-components/32497/sending-data-from-child-to-parent-via -output-event-emitter#t = 201707251354497555945) – Nehal