0

I created a dynamic function and send to an onclick function.

let imagecardnew = new Function('item', 'onclickFunction', 'mainclass', imagecard)
var createdimagecard = imagecardnew(this.cardData[i], this.openModal, this.mainlassName);
document.getElementById('deneme')!.appendChild(createdimagecard)

I am pushing to open modal in onclick function

My modal function:

@ViewChild('modal') private modalComponent!: LargeModalComponent
  modalConfig: ModalConfig = {
    modalTitle: 'Modal',
    dismissButtonLabel: 'string',
    closeButtonLabel: 'string',
    modalClass: 'leftSideBar',
  }
  async openModal(item:any) {
    console.log('deneme',item)
    return await this.modalComponent.open()
  }

openModal function is running and I saw a console.log output, but modalComponent.open throws an error:

ERROR Error: Uncaught (in promise): TypeError: Cannot read properties of undefined (reading 'modalComponent')
TypeError: Cannot read properties of undefined (reading 'modalComponent')

marc_s
  • 704,970
  • 168
  • 1,303
  • 1,425
kubra
  • 3
  • 1
  • 1
    Inside your `openModal()` method, you use `this`. However, in the moment when you pass `this.openModal`, the context is lost. You need to bind it manually: `this.openModal.bind(this)` – JSON Derulo Feb 08 '22 at 13:30
  • Thank you this worked@JSONDerulo – kubra Feb 08 '22 at 13:34

0 Answers0