mismas
mismas

Reputation: 1316

How to use one controller inside another one

I would like to use one controller inside another one. I have found that I could use angular service $controller.

This is what I have tried:

.controller(
'UnplannedTasksController',
[
'$location',
'$uibModal',
'$controller',
'unplannedTasksService',
'messages',
function($location, $uibModal, $controller, unplannedTasksService, messages) {
  var ctrl = this;

  var modalInstanceCtrl = $controller('ModalInstanceCtrl');

  this.openModal = function(unplannedTask) {
    var modalInstance = $uibModal.open({
      animation: false,
      templateUrl: 'unplanned-tasks/unplanned-tasks-modal.html',
      controller: modalInstanceCtrl,
      controllerAs: 'mdCtrl',
      size: 'lm',
      resolve: {
        object: function() {
          return unplannedTask;
        },
        title: function() {
          return messages.unplannedTasks.deleteTitle;
        },
        question: function() {
          return messages.unplannedTasks.deleteQuestion + unplannedTask.partner.name;
        }
      }
    });

    modalInstance.result.then(function(unplannedTask) {
      ctrl.display.getUnplannedTasksBusyPromise = unplannedTasksService.removeUnplannedTask(unplannedTask.id).then(function(data) {
        ctrl.searchUnplannedTasks();
      });
    });
  };
}])

This ModalInstanceCtrl is in another file and look like this:

.controller('ModalInstanceCtrl', function($modalInstance, object, title, question) {

 var ctrl = this;
 this.object = object;
 this.title = title;
 this.question = question;

 this.ok = function() {
 $modalInstance.close(ctrl.object);
 };

 this.cancel = function() {
 $modalInstance.dismiss('cancel');
 };
 })  

But I am getting an error:

"Error: [$injector:unpr] Unknown provider: $modalInstanceProvider <-   $modalInstance <- ModalInstanceCtrl ..."  

Could you please advise. Thank you.

[EDIT] Anyone? Please ...

Best regards,
mismas

Upvotes: 1

Views: 6385

Answers (1)

Deurco
Deurco

Reputation: 530

According to the Angular-UI documentation, you do not need to use $controller. You just have to tell the controller name the the $uibModal service, as a simple string.

In your case:

var modalInstance = $uibModal.open({
    controller: 'modalInstanceCtrl'
    // + more params
});

Upvotes: 2

Related Questions