如何清除ionic2中的所有列表

时间:2016-10-31 10:13:22

标签: angular typescript ionic-framework ionic2 ionic3

我在离子2项目中工作,创建了滑动以删除每个工作正常的项目,还需要单击按钮删除所有项目如何从角度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);
        }
    }

1 个答案:

答案 0 :(得分:3)

您可以像这样创建clear()方法

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

从视图中调用

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