Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to refresh page in ionic 4

Tags:

angular

ionic4

I want to page refresh after successfully deleting the data. When i am deleting or updating then i have to do page refresh then my data refresh . How to use navController in ionic 4 please help me...

book-list.page.html

<ion-header>
  <ion-toolbar>
    <ion-buttons slot="start">
      <ion-menu-button></ion-menu-button>
    </ion-buttons>
    <ion-title>
      Book List
    </ion-title>
    <ion-buttons slot="end">
      <ion-button routerLink="/user-detail">
        <ion-icon name="add-circle"></ion-icon>
      </ion-button>
    </ion-buttons>
  </ion-toolbar>
</ion-header>

<ion-content padding>
  <ion-card>


    <ion-card-content>
      <ion-card-subtitle>Books:</ion-card-subtitle>

      <ion-list *ngFor="let book of books">
          <ion-item>
            <ion-label text-wrap>
                <ion-text>
                    <p>Title: {{book.title}}</p>
                </ion-text>
                <ion-text>
                    <p>Description: {{book.description}}</p>
                </ion-text>
              </ion-label>
              <ion-button [routerLink]="['/edit-book/',book.id]" (click)="editBook(book.id)">
                <ion-icon name="create" slot="end"></ion-icon>
              </ion-button>
              <ion-button color="danger" (click)="delete(book.id)">
                  <ion-icon name="trash" slot="end"></ion-icon>
              </ion-button>

          </ion-item>
        </ion-list>
    </ion-card-content>
  </ion-card>
</ion-content>

book-list.page.ts

import { Component, OnInit } from '@angular/core';
import { Book } from '../book';
import { first } from 'rxjs/operators';
import { UserService } from '../user.service';
import { AlertController } from '@ionic/angular';
import { ToastController } from '@ionic/angular';
import { Router } from '@angular/router';
import {  NavController, NavParams } from '@ionic/angular';
@Component({
  selector: 'app-book-list',
  templateUrl: './book-list.page.html',
  styleUrls: ['./book-list.page.scss'],
})
export class BookListPage implements OnInit {
  books: Book[] = [];
  constructor(private router: Router,public navCtrl: NavController,public toastController: ToastController, private userService: UserService, public alertController: AlertController) { }

  ngOnInit() {
    this.getBook();
  }

  getBook() {
    this.userService.getBook().pipe(first()).subscribe(books => {
      this.books = books;


    });
  }

  async delete(id: number) {

    const alert = await this.alertController.create({
      header: 'Confirm!',
      message: 'Are you sure want to delete this book details?',
      buttons: [
        {
          text: 'Cancel',
          role: 'cancel',
          cssClass: 'secondary',
          handler: (blah) => {
            console.log('Confirm Cancel: blah');
          }
        }, {
          text: 'Okay',
          handler: () => {
            this.userService.delete(id).pipe(first()).subscribe(async books => {

              books = books
              const toast = await this.toastController.create({
                message: 'Your Book details have been deleted.',
                duration: 2000

              });
              toast.present();
              this.router.navigate(['book-list']);
            });
          }

        }
      ]
    });

    await alert.present();

  }
  editBook($id) {
    this.userService.setCurrentId($id);

  }

}

This is my code plase help me how to refresh pages in ionic and how to use NavController in this code please help me

like image 708
Dinesh Vishwakarma Avatar asked Feb 11 '19 09:02

Dinesh Vishwakarma


People also ask

How to implement page refresher in ionic framework?

In the Ionic framework, we can easily implement the page refresher functionally by adding the combination of ion-refresher and ion-refresher-content components. Let’s implement and discuss all configuration by building a real example application 4) How to Change Spinner Icon to Pull Refresh?

What is pull to refresh in Ionic 4?

Ionic 4 | Pull to Refresh functionality in Ionic 4.x Application. As seen in many Android native applications like chrome browser we usually have a user-friendly feature pull to refresh. A user just pulls or simply drag page down to some limit then, the current view is automatically got a refresh.

How do I refresh an ion content element?

Place the ion-refresher as the first child of your ion-content element. Pages can then listen to the refresher's various output events. The refresh output event is fired when the user has pulled down far enough to kick off the refreshing process. Once the async operation has completed and the refreshing should end, call complete ().

How do I add an ion-refresher to a page?

Place the ion-refresher as the first child of your ion-content element. Pages can then listen to the refresher's various output events. The refresh output event is fired when the user has pulled down far enough to kick off the refreshing process.


1 Answers

I solved this problem by moving the function call in the ngOnInit() into ionViewWillEnter(). Try changing your code in book-list-page.ts to:

    ngOnInit() {
    }

    ionViewWillEnter() {
        this.getBook();
    }

I don't know if that's the right or best way to do it but for me the solution works nicely.

like image 182
Edward Chanter Avatar answered Nov 09 '22 03:11

Edward Chanter