In my Ionic 2
app, I have a component that consumes a service which makes an http GET to fetch some data. My component then calls this service and when data is available it sets and presents it.
It looks like following:
export class FarmList implements OnInit {
items: Object;
constructor(public testService: TestService, public nav: NavController){
}
ngOnInit(){
this.getData()
}
getData(){
let loading = Loading.create({
content: 'Please wait..',
spinner: 'crescent'
})
this.nav.present(loading)
this.testService.fetchData().then(data => this.items = data)
}
...
}
While my component fetches the data asynchronously, I am trying to have a loader
spinning and once the data is available, I want the loader
to disappear.
However, with my current code the spinner keeps spinning even after data is available and displayed as can be seen the screenshot:
getData()
is the method that makes service call.
How can I fix this? Is it the correct way to implement loader?
Following code works for me:
ionic 4 - How to implement loading functionality:
I have implemented in my 2-3 projects. It works. BTW in this case i do not need to write below code in every function,
just call
function before execution and call
at the time of execution completion
You can find a working plunker here.
Like you can see in the code of that plunker, I would make a few changes in order to make your code work properly:
================================
UPDATE:
As of the release of Ionic 2.0.0-beta.8 (2016-06-06) changelog:
onPageWillEnter
was renamed toionViewWillEnter