0

I am getting an error for this piece of controller, $http is not defined. Please tell me what's missing..

define(
    ['activityFeedTimeStamp' ],
    function(app) {

        app.register
                .controller(
                        'timeStampController',
                        [
                                '$scope',
                                '$rootScope',

                                function($scope, $rootScope) {
                                    $http.get('http://localhost:7001/ebiz/ebizdashboard/activityfeed/updatetimestamp').
                                          success(function(data, status, headers, config) {

                                          //  $('#timeStampVal').html(data.lastacesstime);
                                            $('#timeStampVal').html(hiiiiiiiii);
                                          }).
                                          error(function(data, status, headers, config) {
                                           $("#timeStamp").hide();
                                          });
                                }]);


    });
KreepN
  • 8,528
  • 1
  • 40
  • 58
NancyK
  • 11
  • 1
  • 1
  • possible duplicate of [AngularJs ReferenceError: $http is not defined](http://stackoverflow.com/questions/13759120/angularjs-referenceerror-http-is-not-defined) – PSL Dec 08 '14 at 18:26

3 Answers3

3

Inject "$http" into the controller like so:

   .controller(
      'timeStampController',
      [
          '$scope',
          '$rootScope',
          '$http', // need to inject $http into controller
      function($scope, $rootScope, $http) {

Basically, any service you use (be it defined by you or built-in Angular one like $http) needs to be injected into a controller to be used.

Since you're using the minify-friendly controller syntax (which lists injections in both an array and the function parameters), you'll need to add it in both places.

See documentation: https://docs.angularjs.org/guide/di (Specifically the section "Inline Array Annotation")

Ghan
  • 797
  • 8
  • 28
2

I have gone through the same problem when I was using

    myApp.controller('mainController', ['$scope', function($scope,) {
        //$http was not working in this
    }]);

I have changed the above code to given below. Remember to include $http(2 times) as given below.

 myApp.controller('mainController', ['$scope','$http', function($scope,$http) {
      //$http is working in this
 }]);

and It has worked well.

source : https://stackoverflow.com/a/22125671/2439715

Community
  • 1
  • 1
Abhishek Goel
  • 18,785
  • 11
  • 87
  • 65
1

You havent injected a $http service in controller

app.register
                .controller(
                        'timeStampController',
                        [
                                '$scope',
                                '$rootScope',
                                '$http'

                                function($scope, $rootScope,$http) {
                                    $http.get('http://localhost:7001/ebiz/ebizdashboard/activityfeed/updatetimestamp').
                                          success(function(data, status, headers, config) {

                                          //  $('#timeStampVal').html(data.lastacesstime);
                                            $('#timeStampVal').html(hiiiiiiiii);
                                          }).
                                          error(function(data, status, headers, config) {
                                           $("#timeStamp").hide();
                                          });
                                }]);


    });
A.B
  • 20,110
  • 3
  • 37
  • 71