search

Home  >  Q&A  >  body text

angular.js - Use angular's factory service to encapsulate $http and ultimately return $$state

Write the data obtained by $http into the factory service of angular.js, and then inject the service into another controller to get the requested data, but the final data obtained is d {$$state: Object}, the specific code Take a look below:

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

The final information returned by the console is as shown below:

Please tell me what’s wrong with my code and how to get the data in the red box in the picture.

PHP中文网PHP中文网2790 days ago794

reply all(1)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-05-15 17:15:19

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

    reply
    0
  • Cancelreply