Var $ http not defined

I am trying to create an Angularjs application and am having problems with my controller.

'use strict';

/* Controllers */

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

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

  });

But when I run it, an error appears indicating that $ http is not defined. What can I improve?

+4
source share
3 answers

You need to enter the service $httpinto the controllers that you use. I like this very accurate built-in syntax that will avoid problems if your code goes through minifiers.

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

    }
])
+6
source
'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;
    });

  });
+4
source

, :

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

All Articles