Daniel Dampf
Daniel Dampf

Reputation: 31

AngularJS: Cannot read property 'get' of undefined $http

var myApp = angular.module('myApp',[]);

myApp.controller('Controller', function($scope, $interval){

    $interval(function($scope, $http){

        $http.get('test.json').success(function(data){
        $scope.notifications = data;
        });
    },5000);

});

Anyone See what exactly I'm doing wrong? The interval is working correctly, as the error repeats ever 5 seconds in Chrome's Console. Am I passing the $http correctly to the controller?

Upvotes: 2

Views: 11600

Answers (4)

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175

you need to pass in $http in your controller, as:

var myApp = angular.module('myApp', []).
myApp.controller('ImagesCtrl', ['$scope', '$http', '$interval', function ($scope, $http, $interval) {
    $interval(function(){

        $http.get('test.json').success(function(data){
        $scope.notifications = data;
        });
    },5000);
}]);

Upvotes: 0

Huy Hoang Pham
Huy Hoang Pham

Reputation: 4147

All Angular module should be injected in the constructor of the controller.

var myApp = angular.module('myApp',[]);
myApp.controller('Controller', function($http, $scope, $interval){

$interval(function(){
    $http.get('test.json').success(function(data){
          $scope.notifications = data;
    });
},5000);

});

Upvotes: 1

Omri Aharon
Omri Aharon

Reputation: 17064

Like you injected the $interval service, you need to inject the $http service:

var myApp = angular.module('myApp',[]);

myApp.controller('Controller', function($scope, $interval, $http){

    $interval(function($scope, $http){

        $http.get('test.json').success(function(data){
        $scope.notifications = data;
        });
    },5000);

});

Upvotes: 0

Steven Wexler
Steven Wexler

Reputation: 17269

You need to inject $http into your controller. (The same way you inject $scope and $interval.) You may be interested to read https://docs.angularjs.org/guide/di.

var myApp = angular.module('myApp',[]);
myApp.controller('Controller', function($scope, $interval, $http){

    $interval(function($scope, $http){

        $http.get('test.json').success(function(data){
        $scope.notifications = data;
        });
    },5000);

});

Upvotes: 0

Related Questions