Skip to content Skip to sidebar Skip to footer

Var $http Not Defined

I'm trying to build an Angularjs application and I'm having trouble with my controller. 'use strict'; /* Controllers */ angular.module('myApp.controllers', []). controller('App

Solution 1:

You need to inject the $http service in the controllers you use it. I like this very precise inline syntax which will avoid problems if your code goes through minifiers.

controller('indexCTRL', ['$scope', '$http',
    function($scope, $http) {
     //...your code

    }
])

Solution 2:

'use strict';

/* Controllers */

angular.module('myApp.controllers', []).
  controller('AppCtrl', function ($scope, $http) {

  }).
  controller('indexCTRL', function ($scope, *$http*) {
    $http.get('/api/frettir').
    success(function(data, status, headers, config) {
      $scope.posts = data;
    });

  });

Solution 3:

You should add it as a dependency to each controller that uses it:

controller('indexCTRL', function ($scope, $http) {

Post a Comment for "Var $http Not Defined"