5
votes

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

1
It is certainly possible with the resolve attribute and I could show the code if you prepare a minimal plunker with what you've tried so far. having said this, your use-case sounds a bit odd. What are you trying to do, functionally speaking? - pkozlowski.opensource
I need to filter items in the modal based on other variables in my main controller. I just need to pass a function with 1 parameter that returns true or false. - mer10z_tech
Wouldn't it be better to pass filtering arguments in this case? This way or another a plunk would get you an answer in no time - pkozlowski.opensource
The filter method is rather complex and used in multiple places. I would rather keep it centrally located in my main controller. I have made a plunk with the solution from @xe4me but it is not yet working for me. - mer10z_tech

1 Answers

8
votes

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
  });