I need to keep the data updated in a child component to do some maths
I have my parent component:
@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;
});
}
And in the view I have:
<list-table [info]="table" (updateTable)="updateTable($event)"></list-table>
So i'm sending the var table to the component list-table
And in my child component I have
@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();
}
}
The issue is that in the line:
var amountofPages = this.info.total % this.info.size == 0 ? ( this.info.total % this.info.size ) : ( this.info.total % this.info.size ) + 1;
the key total is still undefined, because the view is done before the service call is completed.
I tried to do the refresh function with promise, but the result is the same. Is there a way I can wait to complete the variable before sending it to the child compoenent or that the child componet knows when the value was updated so it can execute the math?