Skip to content

Instantly share code, notes, and snippets.

@lazywithclass
Created February 20, 2017 02:34
Show Gist options
  • Save lazywithclass/4fb9da41d1f0383456acc678107d1453 to your computer and use it in GitHub Desktop.
Save lazywithclass/4fb9da41d1f0383456acc678107d1453 to your computer and use it in GitHub Desktop.
Angularjs and unknown provider

Angularjs and unknown provider

Today I think I understood a bit more about

Error: [$injector:unpr] Unknown provider: $modalInstanceProvider <- $modalInstance <- MyController

kind of error.

The situation

I was trying to introduce testing in a project that needed some attention, this is my controller

module myModule {

  'use strict';

  /** @ngInject */
  export class MyController {
   
  public $modalInstance: any;

  /** @ngInject */
  constructor($modalInstance: any) {
    this.$modalInstance = $modalInstance;
  }
    
  // ...

and this is the test setup I wrote

beforeEach(angular.mock.module('myModule'));

beforeEach(inject((($rootScope: any, $controller: any) => {
  var scope = $rootScope.$new();
  myController = $controller('MyController', {
    $scope: scope
  });
})));

They point for me here was that I wasn't understanding this

$modalInstanceProvider <- $modalInstance <- MyController

which means that the $injector is unable to resolve the required dependency, which is the one in the centre.

So all that's to be done is add $modalInstance

beforeEach(inject((($rootScope: any, $controller: any, $modal: any) => {
  var scope = $rootScope.$new();
  UserModalController = $controller('UserModalController', {
    $scope: scope,
    $modalInstance: $modal // <--- THIS
  });
})));

I know this looks silly but took me almost a day to figure out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment