Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a function to angular ui bootstrap modal

What is the best way to pass a function to an angular ui bootstrap modal dialog? I created a method in my modal controller that calls $scope.$parent.myMethod() like so:

$scope.isChosen = function(concept) {
    return $scope.$parent.isChosen(concept);
};

This works, but I would rather pass the function to the modal in a similar way to how functions are passed to directives. I've tried to use the modal "resolve" parameter to do this but without success. Is it possible to resolve a function for a modal, and if so, what is the syntax? If not possible, is there any other way to do this other than accessing the parent scope?

Edit: Here is a plunk attempting to pass a method to a modal, it is a little simplified but represents what I'm trying to do: http://plnkr.co/edit/eCjbZP

like image 797
mer10z_tech Avatar asked Jun 13 '14 12:06

mer10z_tech


1 Answers

When you are defining your modal , you must resolve like this :

  // here is the controller where you want to trigger the modal to open
 $scope.openModal = function () {
     // somewhere in your html , you may click on a button to envoke openModal()
   var modalInstance = $modal.open({
      templateUrl: 'myModalContent.html',
      controller: ModalInstanceCtrl,
      size: size,
      resolve: {
        isChosen: function () {
          return $scope.isChosen;
        }
     }
   });
};

And later on , in your modalCtr you can inject isChosen like this :

  app.controller('modalCtrl',function($scope,isChosen){
     // now you can use your isChosen function however you want
  });
like image 198
Milad Avatar answered Sep 23 '22 02:09

Milad