I am new to Angular and the question may sound stupid. Please bear with me.
I have defined my ngOnInit
like:
ngOnInit() {
this.rowData = this.studentService.getStudents();//Make http calls to populate data
}
And in an event I call ngOnInit again as I need to reload the data:
onSomeEvent(){
this.ngOnInit();
}
Is this OKAY?
Or I should write the line to call http again if ngOnInit()
is a costly method.
No, this is not a good practice.
Better is to call some method from ngOnInit
and reCall the same method when needed. Like this-
ngOnInit() {
this.onLoad();
}
onLoad() {
this.rowData = this.studentService.getStudents();//Make http calls to populate data
}
onSomeEvent(){
this.onLoad();
}
The better way to do it :
ngOnInit(){
this.loadData();
}
//load data
loadData(){
this.rowData = this.studentService.getStudents();
}
//on change event
ngOnChanges(){
this.loadData()
}
//capture data on other event
otherEvent(){
this.loadData()
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With