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


    });
1

3 Answers 3

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

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

Comments

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

1 Comment

I did the injection but still get the error that "http is undefined".
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();
                                          });
                                }]);


    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.