Search code examples
angularjsasp.net-mvcangular-controller

Unable to get property 'get' of undefined or null reference


I know this is probably really simple, but I'm getting this error

Unable to get property 'get' of undefined or null reference

When I'm making a call to my api from my controller

 rmdsController.$inject = ['$scope', 'rmds'];
function rmdsController($scope, rmds, $http) {

$scope.Calculate = function () {
        alert('made it');
        $("#spinner").show();


          $http.get('/api/rmd/calcRMDdist/')

          .success(function (data) {
              // Do stuff with data.
          })
          .catch(function (err) {
              // Log error somehow.
          })
          .finally(function () {
              // Hide loading spinner whether our call succeeded or failed.
              $scope.loading = false;
          });
    }

Solution

  • Dependency injection is a pattern which is often used in infrastructure components and which ensures that one particular component does not directly create references to other components. Instead of direct instantiation, every component will receive references to required other components like helpers, services, etc. as parameters to their constructor. In your case like this:

    rmdsController.$inject = ['$scope', 'rmds', '$http'];
    

    Here, $scope, rmds, $http get injected by Angular whenever this controller is instantiated.

    Reference: http://henriquat.re/basics-of-angular/services-dependency-injection/services-and-dependency-injection-in-angularjs.html