【问题标题】:AngularJS $http is not definedAngularJS $http 未定义
【发布时间】:2013-05-28 18:36:43
【问题描述】:

我对 AngularJS 很陌生。当我打电话给$http.get 时,我收到$http is not defined 错误。

这是我的模块的内容:

var demoApp = angular.module('demoApp', []);

demoApp.config(function ($routeProvider) {
    $routeProvider.
        when('/view1',
        {
            controller: 'SimpleController',
            templateUrl: 'View1.html'
        }).
        when('/view2',
        {
            controller: 'SimpleController',
            templateUrl: 'View2.html'
        })
        .otherwise({ redirectTo: '/view1' });
});

demoApp.factory('simpleFactory', function () {

    var factory = {};
    factory.getAnnounces = function ($http) {
        $http.post("http://localhost:57034/Announce/GetAllAnnounces")
           .success(function (data, status, headers, config) {
               return data;
           }).error(function (data, status, headers, config) {
               return status;
           });
           };
    return factory;
});

demoApp.controller('SimpleController', function ($scope,simpleFactory) {

    $scope.announces = [];
    init();
    function init()
    {
        $scope.announces= simpleFactory.getAnnounces();
    }

});

我在这里缺少什么?干杯。

【问题讨论】:

    标签: angularjs


    【解决方案1】:

    您需要按如下方式检查您的代码:

    demoApp.factory('simpleFactory', ['$http', function ($http) {
    
        return {
            getAnnounces: function () {
                $http.post("http://localhost:57034/Announce/GetAllAnnounces")
                   .success(function (data, status, headers, config) {
                       return data;
                   }).error(function (data, status, headers, config) {
                       return status;
                   });
            }
        };
    
    }]);
    

    没有必要在getAnnounces方法定义中传递$http变量,因为它已经在工厂函数的范围内定义了。

    我正在为 AngularJS 使用参数别名以避免缩小器出现问题,请参阅 AngularJS web site 上的“关于缩小的说明”。

    请注意,$http.post.success$http.post.error异步的,除非您使用 Promise ($q),否则您将无法获取数据,请参阅 here。因此,您可以这样更改代码:

    demoApp.factory('simpleFactory', ['$http', '$q', function ($http, $q) {
    
        return {
            getAnnounces: function () {
                var deferred = $q.defer();
    
                $http.post("http://localhost:57034/Announce/GetAllAnnounces")
                   .success(function (data, status, headers, config) {
                       deferred.resolve(data);
                   }).error(function (data, status, headers, config) {
                       deferred.reject(data);
                   });
    
                return deferred.promise;
            }
        };
    
    }]);
    

    SimpleController:

    demoApp.controller('SimpleController', ['simpleFactory', '$scope', function (simpleFactory, $scope) {
    
        $scope.announces = []; 
    
        simpleFactory.getAnnounces()
            .then(function(data) {
                // call was successful
                $scope.announces = data;
            }, function(data) {
                // call returned an error
                $scope.announces = data;
            });
    
    }]);
    

    【讨论】:

      猜你喜欢
      • 2012-11-25
      • 1970-01-01
      • 2016-05-09
      • 1970-01-01
      • 2013-03-07
      • 2015-04-02
      • 2020-01-16
      • 2019-11-15
      • 1970-01-01
      相关资源
      最近更新 更多