我需要保持在一個子組件更新的數據做一些數學的最新數據如何獲得的子組件angular2
我有我的父組件:
@Component({
selector: 'car-brand-index',
templateUrl: './index.component.html',
styleUrls: ['./index.component.scss'],
providers: [CarBrandService, Response, SessionService, ServerService, ListTableComponent, TableAction, Table, TableRow, TableRowField]
})
export class CarBrandIndexComponent implements OnInit {
table = new Table();
constructor(private mainService : CarBrandService) {
this.refresh(this.table.page,this.table.size);
}
refresh(pageNumber : number, pageSize : number){
this.mainService.get(pageNumber, pageSize).then(result => {
this.table.total = result.data.total;
});
}
並在視圖我有:
<list-table [info]="table" (updateTable)="updateTable($event)"></list-table>
所以我送無功表組件列表表
在我的卡LD組件我有
@Component({
selector: 'list-table',
templateUrl: './list-table.component.html',
styleUrls: ['./list-table.component.scss'],
providers: [ListTableService]
})
export class ListTableComponent implements OnInit {
@Input()
info: Table;
@Output()
updateTable = new EventEmitter();
pages : number[] = [];
constructor(public listTableService : ListTableService) {
}
updateFooter() {
if(this.info){
var amountofPages = this.info.total % this.info.size == 0 ? (this.info.total % this.info.size) : (this.info.total % this.info.size) + 1;
for(var i = 1; i <= amountofPages; i++){
this.pages.push(i);
}
}
}
ngOnInit(){
this.updateFooter();
}
}
的問題是,在該行:
var amountofPages = this.info.total % this.info.size == 0 ? (this.info.total % this.info.size) : (this.info.total % this.info.size) + 1;
關鍵總還是不確定的,因爲在完成呼叫維修服務前視圖完成。
我試着用promise來做刷新函數,但結果是一樣的。有什麼方法可以等待在將變量發送給子組件之前完成該變量,或者子組件知道何時更新了值以便它可以執行數學運算?
使用服務,與已發表的觀察到的事件。組件可以訂閱這些事件,並在事件觸發時執行數學計算。我認爲Flux模式可以幫助你:http://stackoverflow.com/questions/42219858/how-can-i-maintain-the-state-of-dialog-box-with-progress-all-over-my-angular -2-a/42221273?noredirect = 1#comment71607237_42221273 – pixelbits
我正在使用服務從後端獲取所有數據...所以,您的意思是我需要爲子組件提供服務,以便父服務器存儲信息那裏? 但在我的情況下,數據正在更新,但updateFooter()函數不會在該更新後觸發。所以var var ofofages is outofdate – Faabass
如果不是,我可以做的就是對父項進行數學計算,然後發送最終值,這樣我就不需要任何子函數,但不是最好的方法 – Faabass