Search code examples
javascriptangularjsdependency-injectionstrict

How do I fix "Uncaught TypeError: can't access property "whenGET", $httpBackend is undefined"?


I am trying to use the following https://embed.plnkr.co/plunk/pWNOdA in a project that uses strict-DI.

In the app-mockbackend.js you can see the following setup -

angular.module('app').run(function($httpBackend, ServerDataModel) {
    
    $httpBackend.whenGET('/games').respond(function(method, url, data) {
        var games = ServerDataModel.findAll();
        return [200, games, {}];
    });
}]);

When you run the app without strict it runs fine, but when you add ng-strict-di to your app I get:

Uncaught TypeError: can't access property "whenGET", $httpBackend is undefined

I have read the docs and with other areas, I have had to annotate services while using strict and the errors have subsided and it's great - I just cannot seem to figure this one out.


Solution

  • Hey Dale did you try to annotate the function?

    https://docs.angularjs.org/guide/di#dependency-annotation

    For example:

    someModule.controller('MyController', ['$scope', 'greeter', function($scope, greeter) { // ... }]);