2
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?

2
  • 2
    you don't appear to be passing $http into the controller at all.... Commented Mar 10, 2015 at 6:07
  • create a factory and have a method to do the http get and invoke that method over here, after injecting the factory in the controller. as @Claies mentioned in this example you forgot to inject $http Commented Mar 10, 2015 at 6:11

4 Answers 4

1

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);

});
Sign up to request clarification or add additional context in comments.

Comments

0

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);

});

Comments

0

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);

});

Comments

0

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);
}]);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.