0

I'm trying run a class method (in the controller that creates my $uibModal) once the modal's "okay" button has been pressed. My understanding is that the modalInstance is returning a modal object from the component, and therein lies my problem... how can I interact with the parent controller instead of the component?

Example:

modalInstance.result.then(function () {
      // if close() called in component, do this in the parent controller
      this.users.splice(this.users.indexOf(user), 1);    
   }, function () {
      // if dissmised, do this
      console.log('modal-component dismissed at: ' + new Date());
});

This throws an error:

TypeError: Cannot read property 'users' of undefined

Here is a Plunker with the key items: https://plnkr.co/edit/ZY3Kr9g3MuT1AxsQKfsj?p=catalogue

AngularJS 1.6, using Angular Fullstack Generator 4.2.3

I've followed this documentation: https://angular-ui.github.io/bootstrap/ to create a modal component and the modal works great. But I can't splice my array of users no matter what I try. Please help!

Omar Einea
  • 2,478
  • 7
  • 23
  • 35
rjkunde
  • 531
  • 1
  • 4
  • 12
  • Your plunker is broken, but `TypeError: Cannot read property 'users' of undefined` usually points out to incorrect `this` binding. Double check it. – Karen Grigoryan Mar 04 '18 at 21:07
  • I'm a little stuck with getting plunker to run (there are several parts of the app omitted; I was just using plunker (perhaps incorrectly) to show the component, controller, and modal html in my question. What should I be double checking? Oddly, if I call splice called outside of modalInstance.result.then(), it works flawlessly, but seems to get lost since the modalInstance returns an object. – rjkunde Mar 04 '18 at 22:07
  • check my answer below pls – Karen Grigoryan Mar 04 '18 at 22:39

1 Answers1

1

Might be because of incorrectly bound this in your callback, try binding it explicitly to parent enclosing scope using =>:

modalInstance.result.then(() => {
      // if close() called in component, do this in the parent controller
      this.users.splice(this.users.indexOf(user), 1);    
   }, function () {
      // if dissmised, do this
      console.log('modal-component dismissed at: ' + new Date());
});
Karen Grigoryan
  • 5,234
  • 2
  • 21
  • 35