sridharan sridharan - 1 month ago 7
TypeScript Question

how to clear all list in ionic2

I'm working in ionic 2 project, created swipe to delete each items working fine and also need click button delete all items How can I delete all items from a list in angular 2?

<button ion-button clear>Clear All</button>
<ion-item-sliding *ngFor="let note of notes">
<ion-item>
{{note.title}}
</ion-item>
<ion-item-options>
<button (click)="deleteNote(note)" danger>
<ion-icon name="trash"></ion-icon>
</button>
</ion-item-options>
</ion-item-sliding>


file.ts

constructor( public viewCtrl: ViewController ) {
this.notes = [
{ title: 'This is notification swipe to delete' },
{ title: 'This is notification swipe to delete' }
];
}
deleteNote(note){

let index = this.notes.indexOf(note);

if(index > -1){
this.notes.splice(index, 1);
}
}

Answer

You can create a clear() method like this

public clear(): void {
  this.notes = [];
}

And call it from the view

<button ion-button clear (click)="clear()">Clear All</button>  
Comments