Angular2 cannot access 'this' inside a promise

前端 未结 3 1617
伪装坚强ぢ
伪装坚强ぢ 2021-01-05 21:27

I am unable to call a function inside promise of ng2-sweetalert2 plugin

swal({
    title: \'Are you sure?\',
    text: \"You won\'t be able to revert this!\"         


        
3条回答
  •  萌比男神i
    2021-01-05 21:49

    this is because this refers to the promise itself. do this :

    let self = this;
       swal({
        title: 'Are you sure?',
        text: "You won't be able to revert this!",
        type: 'warning',
        showCancelButton: true,
        confirmButtonText: 'Yes, delete it!'
    }).then(function(x) {
        self.removeNote(key);
        swal(
        'Deleted!',
        'Your file has been deleted.',
        'success'
        );
    }, function(e){
           console.log('Cancelled');
    });
    
    removeNote(key){
        this.todo.remove(key);
        this.afService.closeNote(key);
    }
    

提交回复
热议问题