0

I'm developing a simple modal window with ui.bootstrap. This modal is showed when we click in a certain button binding to a controller and fires up, but the modal and its content is binding to another controller so when we click is necessary to know where the controller is which it'll be in another folder of the project.

For example, imaging the structure as follows:

  1. component1

    ..... template1.html

    ..... controller1.js

  2. component2

    ..... template2.html

    ..... controller2.js

The controller1.js is in charge of load the modal view which renders and is binding with template2.html and controller2.js respectively. So, in controller1.js we have this:

 $scope.open = function (size) {
      var modalInstance = $uibModal.open({
        templateUrl: 'components/component2/template2.html',
        controller: 'components/component2/controller2.js',
        size: size,
        resolve: {
          items: function () {
            return $scope.items;
          }
        }
      });
      modalInstance.result.then(function (selectedItem) {
        $scope.selected = selectedItem;
        $log.debug(selectedItem);
      }, function () {
        $log.info('Modal dismissed at: ' + new Date());
      });
   };

Which obviously does not work for controller2.js. As we do we templateUrl, there is any way to load a controller passing its path as parameter in the $uibModal.open?

Enot
  • 790
  • 2
  • 15
  • 34

1 Answers1

1

I have not tested it, but do something like:

var modalInstance = $uibModal.open({
        templateUrl: 'components/component2/template2.html',
        controller: 'ModalController',
        size: size,
        resolve: {
          items: function () {
            return $scope.items;
          }
        }
      });

   app.controller('ModalController', function ($scope, $modalInstance) {
      // do some things
   });
Develobba
  • 716
  • 8
  • 22
  • That's what I'm trying so far and it does not work :( I guess it's not in the same 'scope' and I need to 'inject' the modal controller in some way... – Enot Feb 23 '16 at 09:20
  • the $scope in ModalController is the scope of the modal, not of your controller. To get the $scope of your controller, you have to pass it to your modal like: resolve: { scope: function () { return $scope; } } and inject 'scope' in your ModalController. Maybe adjust the naming, but it should be done in this way. Let me know if not. – Develobba Feb 23 '16 at 09:21
  • I guess I'm not explaining properly well... I don't actually want to get `$scope` from my controller and pass it and injecting in modal controller. What I'm really trying to accomplish is trying to call the modal controller in `controller2.js` in order to instantiate the modal and fires up in `controller1.js` but always get `ModalController is undefined or not a function`. – Enot Feb 23 '16 at 09:34
  • In addition, both controllers are in separate folders. – Enot Feb 23 '16 at 09:38
  • 1
    Folders and directories are not necessary. You have to import them in your index.html, so different paths does not matter. You want to access a controller from a other controller as far i can see. http://stackoverflow.com/questions/9293423/can-one-controller-call-another – Develobba Feb 23 '16 at 09:40
  • 'You have to import them in your index.html' along with defining it the controller in the app module definition which I forgot made it the work. Thanks a lot! Three hours wasted trying to figure out... – Enot Feb 23 '16 at 09:52