错误: - Angular JS中未定义$ http

时间:2022-02-02 11:22:41

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

我收到这个控制器的错误,$ http没有定义。请告诉我缺少什么..

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


    });

3 个解决方案

#1


3  

Inject "$http" into the controller like so:

将“$ http”注入控制器,如下所示:

   .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.

基本上,您使用的任何服务(由您定义或内置Angular,如$ http)都需要注入要使用的控制器。

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.

由于您使用的是minify-friendly控制器语法(在数组和函数参数中列出了注入),因此您需要在两个位置添加它。

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

请参阅文档:https://docs.angularjs.org/guide/di(特别是“内联数组注释”部分)

#2


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.

我已将上面的代码更改为以下内容。请记住包含$ http(2次),如下所示。

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

and It has worked well.

它运作良好。

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

来源:https://*.com/a/22125671/2439715

#3


1  

You havent injected a $http service in controller

你没有在控制器中注入$ http服务

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


    });

#1


3  

Inject "$http" into the controller like so:

将“$ http”注入控制器,如下所示:

   .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.

基本上,您使用的任何服务(由您定义或内置Angular,如$ http)都需要注入要使用的控制器。

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.

由于您使用的是minify-friendly控制器语法(在数组和函数参数中列出了注入),因此您需要在两个位置添加它。

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

请参阅文档:https://docs.angularjs.org/guide/di(特别是“内联数组注释”部分)

#2


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.

我已将上面的代码更改为以下内容。请记住包含$ http(2次),如下所示。

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

and It has worked well.

它运作良好。

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

来源:https://*.com/a/22125671/2439715

#3


1  

You havent injected a $http service in controller

你没有在控制器中注入$ http服务

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


    });