Credit to

angularjs 1.4


Improve this Doc View Source $controller

  1. service in module ngMock

A decorator for $controller with additional bindings parameter, useful when testing controllers of directives that use bindToController.

// Directive definition ...

myMod.directive('myDirective', {
  controller: 'MyDirectiveController',
  bindToController: {
    name: '@'

// Controller definition ...

myMod.controller('MyDirectiveController', ['$log', function($log) {

// In a test ...

describe('myDirectiveController', function() {
  it('should write the bound name to the log', inject(function($controller, $log) {
    var ctrl = $controller('MyDirectiveController', { /* no locals */ }, { name: 'Clark Kent' });
    expect('Clark Kent');
    expect($['Clark Kent']);


$controller(constructor, locals, [bindings]);


Param Type Details
constructor function()string

If called with a function then it's considered to be the controller constructor function. Otherwise it's considered to be a string which is used to retrieve the controller constructor using the following steps:

  • check if a controller with given name is registered via $controllerProvider
  • check if evaluating the string on the current scope returns a constructor
  • if $controllerProvider#allowGlobals, check window[constructor] on the global window object (not recommended)

    The string can use the controller as property syntax, where the controller instance is published as the specified property on the scope; the scope must be injected into locals param for this to work correctly.

locals Object

Injection locals for Controller.


Properties to add to the controller before invoking the constructor. This is used to simulate the bindToController feature and simplify certain kinds of tests.



Instance of given controller.

© 2010–2017 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.$controller