Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ionic 2 Refresher toggle ion-refresher on page load

Overview:

I make a network request to fill a list when my page loads in an Ionic 2 app. At this moment I am toggling a ion-spinner when the page initially loads.This is the refresher currently:

<ion-refresher (ionRefresh)="doRefresh($event)">

  <ion-refresher-content
    pullingText="Pull to refresh..."
    refreshingSpinner="circles">
  </ion-refresher-content>

</ion-refresher>

<ion-spinner *ngIf="loading" color="light" name="circles"></ion-spinner>

My desired behavior would be to remove the ion-spinner component and just enable the ion-refresher when the page initially loads. I've seen a couple examples of this with Ionic V1, but I cant seem to translate it over to Angular 2.

Question:

Is there a way to trigger the ion-refresher and its spinner from my controller (*.ts), so I can remove the extra ion-spinner component from my template?

like image 668
Pezetter Avatar asked Mar 14 '17 22:03

Pezetter


People also ask

How do you refresh an ionic page?

In this HTML file, we need to add <ion-refresher> component where we call the Refresh event. Inside this component, add the <ion-refresher-content> sub-component which contains the custom refresher properties.

What is the purpose of the ion refresher component in ionic?

The refresher provides pull-to-refresh functionality on a content component. The pull-to-refresh pattern lets a user pull down on a list of data using touch in order to retrieve more data. Data should be modified during the refresher's output events.

What is ion refresher?

ion-refresher The Refresher provides pull-to-refresh functionality on a content component. Place the ion-refresher as the first child of your ion-content element. Pages can then listen to the refresher's various output events.


1 Answers

I actually found a hack:

import { Content, Refresher } from 'ionic-angular';

export class MyPage {
  @ViewChild(Content) content: Content;
  @ViewChild(Refresher) refresher: Refresher;
...
  ionViewDidEnter() {
    this.refresher._top = this.content.contentTop + 'px';
    this.refresher.state = 'ready';
    this.refresher._onEnd();
  }

This will show and trigger the refresher. It implicitly calls the registered refresh function.

like image 121
thymythos Avatar answered Oct 08 '22 18:10

thymythos