NancyK
NancyK

Reputation: 11

ERROR:- $http is undefined in Angular JS

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


    });

Upvotes: 0

Views: 9856

Answers (3)

Abhishek Goel
Abhishek Goel

Reputation: 19771

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

Upvotes: 2

Ghan
Ghan

Reputation: 827

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

Upvotes: 3

A.B
A.B

Reputation: 20455

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


    });

Upvotes: 1

Related Questions