Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
355 views
in Technique[技术] by (71.8m points)

使用angular的factory服务封装$http,最终返回$$state

在angular.js 的 factory服务里写入$http得到的数据,然后在另一个controller里面,注入该服务,就能得到请求的数据,但是最终得到的数据却是d {$$state: Object},具体代码看下面:

app.controller("MainCtrl",["$scope","getData",function ($scope,getData) {
                console.log(getData.getHttp());
        }]);
app.factory("getData",["$http",function($http) {
            return {
                getHttp:function () {
                    return $http.get("data/article.json")
                    .then(function(res) {
                        return res.data;
                    },function(error) {
                        return error;
                    })
                }
            }
        }]);

最后控制台返回的信息如下图所示:
$$state

请教各位大神,我的代码哪里出问题了,怎样才能得到图中红色框里的数据。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
app.controller("MainCtrl",["$scope","getData",function ($scope,getData) {
            getData.getHttp().then(function(res) {
                        console.log(res.data);
                    },function(error) {
                        console.log(error);
                    });
        }]);
        
app.factory("getData",["$http",function($http) {
            return {
                getHttp:function () {
                    return $http.get("data/article.json")
                }
            }
        }]);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...