我有以下工厂定义:
angular.module("account").factory("users",["$http",
function(a){
return {
getUser: function(){
return a.get("/user/me").then(function(r){
return r.data;
});
}
};
}
]);
我的控制器:
angular.module("test.controllers",["account"])
.controller("TestCtrl",["$scope","users",
function(a,u){
a.user = u.getUser();
console.log(a.user);
}]);
这是console.log的输出:
d {$$state: Object, then: function, catch: function, finally: function} $$state: Object status: 1 value: Object user: Object__v: 0 _id: "54c1fg29f36e117332000005" temp: "1ce3793175e0b2548fb9918385c2de09" __proto__: Object __proto__: Object __proto__: Object __proto__: Object
上述代码返回的是状态对象而不是用户对象。但从日志中可以看到,状态对象中包含了用户对象。我该如何获取用户对象?还是说我完全弄错了?
我知道另一种方法是返回 $http.get 并在控制器内调用 then() 方法。但是我将频繁使用用户对象,如果在控制器中调用 then() 方法,几乎和在工厂中使用 $http.get 相同。