Use $componentController in production?

113 Views Asked by At

I am building a dialog service. A dialog can have a controller, very similar to $mdDialog, like this:

myDialogService.show({
  templateUrl: `<div ng-click="$ctrl.log()">Hello dialog</div>`,
  controller: function() {
    this.log = function() {
      console.log("logged from myDialogController");
    }
  }
});

which works great. I invoke the controller that way:

locals.$scope = scope;
const invokeController = $controller(options.controller, locals, true);

const controller = invokeController();
if (options.controllerAs) {
  scope[options.controllerAs] = controller;
} else {
  const controllerAs = "$ctrl";
  scope[controllerAs] = controller;
}

In angular-mock is the $componentController service, which can invoke component controllers. With my code, I only can invoke registered controllers, or given controller functions. This is not very helpful, as I only have components registered, not single controllers.

My question

Is it possible/recommended to use the $componentController in production? Or is there any AngularJS build in variant I have overseen?

1

There are 1 best solutions below

2
On BEST ANSWER

$componentController belongs to ngMock module because it is useful for testing but is considered a hack in production. Since ngMock is big enough and isn't supposed to be available in production, it should be pasted in order to become available.

The proper way to solve this is to either have registered controllers that are reused as component controllers or import/export controller functions/classes with JS modules.

Since MdDialogController belongs to third-party module, isn't registered or exported and is small, it can be just pasted.