[英]How to pass multiple data back from child to parent component in angular?
目前,我在學校項目中使用的是角度4。 我有一個數組,每個項目都是一個子組件,可以更新和刪除它,這意味着我應該知道索引和數據。
parent.ts:
updOne(i:number,stc:string):void{
this.myarray[i]=stc
}
delete(edu:string):void{
this.myarray=this.myarray.filter(x=>x!==edu)
}
parent.html:
<child-com [edu]=x [num]=i (updstr)="updOne($event)" (delstr)="delete($event)"></child-com>
兒童com.ts:
@Input() edu:string
@Input() num:number
@Output() updstr: EventEmitter<string> = new EventEmitter<string>()
@Output() delstr: EventEmitter<string> = new EventEmitter<string>()
//some other code here
save():void{
this.updstr.emit(this.edu)
this.updating=false
}
del():void{
this.delstr.emit(this.edu)
}
毫無疑問,刪除效果很好。 問題正在更新。 實際上,使用* ngFor,trackBy並手動將其全部打印出來,就可以解決此問題。 但是我想嘗試使用子組件,就像在React中一樣。 當我玩React時,我可以只使用javascript閉包,即myfunc.bind(this,i,stc)。
我嘗試在這里使用綁定,沒有結果
使用bind時的代碼:
parent.ts:
@Output() updstr: EventEmitter<number,string> = new EventEmitter<number,string>()
parent.html:
//I've tried some order
//this,i,$event
//$event,this,i
<child-com [edu]=x (updstr)="updOne.bind(this,$event,i)" (delstr)="delete($event)"></child-com>
而且打字稿中的泛型不允許多個數據,所以我不能發出多個數據
所以我的問題是,如何使用發射或綁定將一些數據從孩子一次傳遞給父母?
感謝Alex,使用對象可以替代多次數據傳遞。 只是為了確保數據正確,使用了接口,就像這樣
export interface Interview{
num:number
payload:{
dt:string
seeker:string
}
}
並像這樣使用
@Output() updstr: EventEmitter<Interview> = new EventEmitter<Interview>()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.