5

I have a component where i pass what i want to display inside a modal like this :

  openConfirmModal() {
    this.service.openmodal(
      {
        buttons: [ {name: 'Close'} ]
      }
    );
  }

The modal service is like this :

  openmodal(input: String) {
    const dialogRef = this.dialog.open(popupComponent, {
      data: new ModalConfirmData({
        buttons: Object.values(data)[0]
      })
    });
  }

Inside my popupComponent i have :

export class ModalPopupData {
  actions: Array<Object>;

  constructor(input?) {
    if (input) {
      this.buttons = input.buttons;
    }
  }
}

Now everything works fine with this, but what i want to do right now is to pass a function to my service in order for it to consume it. Something like this but i don't know how :

{
  function: (modalComponent) => {
    modalComponent.close();
  }
}
3
  • You can try removing async from the callback definition and handle the click event with: (click)="act.callback(this)". Commented Apr 21, 2019 at 2:30
  • it's not working :/ Commented Apr 21, 2019 at 2:54
  • Can you post the problem in stackblitz so we can solve it better? Because you can pass a function as a callback parameter without problem. Commented Apr 30, 2019 at 9:11

3 Answers 3

5

Your callback is expecting to take in a modalComponent as a parameter, but your html is doing nothing of the sort at just (click)="act.callback".

In your current code, you're constructing a method that looks, essentially, like this:

var myCallback = function(modalComponent) {
  modalComponent.close();
}
// ...
var someObj = {
  name: "Close",
  callback: myCallback
}

And so calling it, something would eventually need to do:

// ...
var modalComp = getComponent(); // however it gets hold of a modalComponent
someObj.callback(modalComp);

The method needs to be given a modalComponent to work on, but when calling it, you are not giving it one - in your current code.

So, either your current calling of the callback needs to give the modal you want to close as the input (akin to the comment by @ConnorsFan, assuming this is the relevant reference to the modal), or you change your method to already know about the modal.

var myModal = ...;
var myObj = {
  name: "Close",
  callback: () => { myModal.close(); }
}

Now usage just looks like this, as there is no input params:

myObj.callback();

Anonymous functions are useful for cheekily making use of resources that don't really exist in the caller's scope.

Sign up to request clarification or add additional context in comments.

2 Comments

but when i do so, i get an error that ERROR Error: Uncaught (in promise): TypeError: modalComponent.close is not a function
Does your modal component expose a public close() method? How are you gaining that reference to the modalComponent?
4
+25

Just like your buttons, you can pass your methods/callbacks to your service and then call them from within the service, e.g.

openConfirmModal() {
  this.service.openmodal({
    buttons: [ {name: 'Close'} ],
    methods: {
      onClose: () => {/** your function body goes here */},
      onSubmit: () => {/** your function body goes here*/}
      .
      .
      .
    }
  });
}

And then in your service, you can call them whenever you like.

Check this also click

Comments

2

You can use simple javascript/typescript to solve this, using just a callback method.

Your service function would be something like this:

serviceFunction(callback?) {
  const cb = callback || (() => { });
  // do your services thing
  return cb();
} 

So when you call it you just call the serviceFunction with the callback function you want to use.

Considering that you have a this.close() method in your component, the call would look like:

myService.serviceFunction(this.close);

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.