Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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('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 saying $http is not defined. What can I improve?

like image 259
sverrirarnors Avatar asked Dec 14 '22 14:12

sverrirarnors


1 Answers

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

    }
])
like image 176
ryanyuyu Avatar answered Dec 28 '22 17:12

ryanyuyu