0
declare var alertify:any;
export class ExamplComponent implements OnInit {
    itemList = []

    deleteItem(idx) {
    alertify.confirm('Are you sure delete this record.', function() {
      this.itemList.slice(idx,1);
    })
  }
}

html Page

< div *ngFor="let item of itemList;let indx = index" style="text-align: center">
    <button class="text-btn text-danger" (click)="deleteItem(indx)" ><i class="fa fa-trash"></i></button>
</div>

Source Library: https://alertifyjs.com/confirm.html

Error Message

ERROR TypeError: Cannot read property 'itemList' of undefined

2 Answers 2

1

use fat arrow => instead of function

deleteItem(idx) {
    alertify.confirm('Are you sure delete this record.', ()=> {
      this.itemList.slice(idx,1);
    })
}
Sign up to request clarification or add additional context in comments.

Comments

1

The problem is on the function passed as argument to alertify.confirm(). In this case when you declare a function using function() it has its own this; therefore, when you code this.itemList you are pointing to undefined as it doesn't exist on your function.

Try to use arrow function instead (() => {}), they inherit the this from the current scope, so it is safe to use within a callback.

deleteItem(idx) {
    alertify.confirm('Are you sure delete this record.', () => {
      this.itemList.slice(idx, 1)
    })
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.