【问题标题】:AngularJS - Cannot read property of 'get' undefinedAngularJS - 无法读取'get'未定义的属性
【发布时间】:2017-04-28 20:43:04
【问题描述】:

我正在尝试检索一个虚拟 JSON 文件,但我不断收到 'TypeError Cannot read property of 'get' undefined.

这似乎是什么问题?

模块:

angular.module('followup',['followupSearch']);

工厂:

angular.module('followup')
    .factory('followupStore', ['$http', function($http) {
        var followup = {};

        followup.getFollowup = function($http) {
            return $http.get('https://jsonplaceholder.typicode.com/posts');
        };

        return followup;
    }]);

控制器:

angular.module('followupSearch')
.controller('followupSearchCtrl', ['$http','followupStore',  function($http, followupStore) {

    var self = this;

    self.getFollowup = getFollowup;

        // Get followup
            function getFollowup() {
               followupStore.getFollowup()
                    .then(function (response) {
                        console.log('success');
                    }, function (error) {
                        console.log('error');
                    });
            } //getFollowup

}]);

【问题讨论】:

  • getFollowup 将 $http 作为输入参数。删除它。

标签: javascript angularjs components


【解决方案1】:

只需删除工厂函数的参数即可。它会起作用,因为您已经在工厂中注入了$http 服务:

angular.module('followup')
    .factory('followupStore', ['$http', function($http) {
        var followup = {};

        followup.getFollowup = function() {
            return $http.get('https://jsonplaceholder.typicode.com/posts');
        };

        return followup;
}]);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-04
    • 2021-04-29
    • 2021-12-01
    • 1970-01-01
    • 2016-10-23
    • 2019-09-16
    相关资源
    最近更新 更多