3

I’m building a favorite list which I can erase list items within a page. It’s based on Ionic sqlite storage.

I found Ionic page will keep removed ion list items until I re-enter the list page. So it needs refreshing a page upon removing an item from ionic storage.

How can I refresh a particular page? window.location.refresh() seems to refresh the entire pages… this is not what I’m trying to do.

I need to refresh a single page out of twenty pages on my app so navCtrl.setRoot() will not work either.

Thanks in advance,

Pradeep
  • 9,667
  • 13
  • 27
  • 34
jamesharvey
  • 165
  • 1
  • 3
  • 18
  • 1
    You need'nt refresh the page, you need refresh the "data". You can do calling again to the function that subscribe to the get, or splice the array of your data at time you receive that the erase item is completed. Another idea is that you delete function return the list completed, but in Angular rarely you need refresh a page. Yes, I know, it's not an answer is say to you: re-think the problem. – Eliseo May 01 '18 at 11:40
  • any code example for using subscribe and splice? thanks for your advice. – jamesharvey May 01 '18 at 11:51

3 Answers3

3

Please Try this?

this.navCtrl.setRoot(this.navCtrl.getActive().component);
keval nayak
  • 254
  • 1
  • 2
  • 11
2

If you're going for a list of items, I would suggest displaying them with an ngFor directive. That way when you update the list, it will automatically be updated where it is displayed rather than having to refresh the page or anything like that. Here's a short example:

HTML

<ion-content>
 <ion-list> 
  <div *ngFor="let item of items">
    <p>{{item.name}}</p>
  </div>
  <button (click)="deleteFromArray()">Delete From Array</button>
  <button (click)="addToArray()">Add To Array</button>
 </ion-list> 
</ion-content>

TYPESCRIPT

items: any[] = [{name:'Penguin'},{name:'Seal'},{name:'Lion'}];

deleteFromArray() {
  this.items.pop();
}

addToArray() {
  this.items.push({name: 'Whale'});
}

Hope this helps!

mclem
  • 128
  • 7
  • Thanks but this.items.pop() throws me 'cannot delete undefined' error.. any other suggestions? – jamesharvey May 01 '18 at 12:56
  • I tried to replicate your error, but couldn't seem to get that error :/ I would suggest filtering out the undefined values in the array like this guy did: https://stackoverflow.com/questions/9596124/javascript-how-to-clear-undefined-values-from-an-array or having your own way of ensuring that you're not trying to delete an undefined value. – mclem May 01 '18 at 13:10
0

use NgZone. It will refresh the component.

import { NgZone } from '@angular/core';

constructor(private zone: NgZone) {}

refresh() {
  this.zone.run(() => {
    console.log('force update the screen');
  });
}
shanthan
  • 554
  • 8
  • 8