2

Is there a way to get reference to the currently instantiated controller object from within the controller's definition? I'd like to $compile a modal and bind that to the same controller that's creating the modal.

Here's a simplified version of what I'd like to do, where THIS_CONTROLLER_INSTANCE is a reference to my controller instance.

angular.module('foo')
.controller('barController', function($scope, $rootScope){
  $scope.openModal = function(){
    var modalEl = $('<div class="modal">Modal stuff here</div>');
    var controller = THIS_CONTROLLER_INSTANCE;
    modalEl.contents().data('$ngControllerController', THIS_CONTROLLER_INSTANCE);

    $compile(modalEl)($scope);
    $('body').append(modalEl);
  }
});
Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265
bioball
  • 1,339
  • 1
  • 12
  • 23

1 Answers1

0

As noted above:

The current controller is this. So the solution to my problem is:

angular.module('foo')
.controller('barController', function($scope, $rootScope){
  var self = this;
  $scope.openModal = function(){
    var modalEl = $('<div class="modal">Modal stuff here</div>');
    var controller = self;
    modalEl.contents().data('$ngControllerController', controller);

    $compile(modalEl)($scope);
    $('body').append(modalEl);
  }
});
Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265