【问题标题】:ERROR:- $http is undefined in Angular JS错误:- $http 在 Angular JS 中未定义
【发布时间】:2015-02-06 10:53:37
【问题描述】:

我收到这个控制器的错误,$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();
                                          });
                                }]);


    });

【问题讨论】:

标签: angularjs


【解决方案1】:

你还没有在控制器中注入 $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();
                                          });
                                }]);


    });

【讨论】:

    【解决方案2】:

    像这样将“$http”注入控制器:

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

    基本上,您使用的任何服务(无论是您定义的还是内置的 Angular 服务,如 $http)都需要注入到控制器中才能使用。

    由于您使用的是对 minify 友好的控制器语法(在数组和函数参数中都列出了注入),因此您需要在这两个地方添加它。

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

    【讨论】:

      【解决方案3】:

      我在使用的时候也遇到了同样的问题

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

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

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

      而且效果很好。

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

      【讨论】:

      • 我进行了注入但仍然收到“http未定义”的错误。
      猜你喜欢
      • 2013-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多