【发布时间】:2016-02-23 22:54:59
【问题描述】:
所以我在服务器中有一组对象,我想在页面加载时填充 ng-repeat。
我做了一个工厂,它从服务器上的资源中获取列表,如下所示:
app.factory('objectArray', ['$http', function($http) {
// This is returning a $$state object
// instead of response.data...
return $http.post('/get_collection').then(function(response) {
console.log(response.data);
return response.data;
});
}]);
之前在状态声明中使用 ui-router 和 resolve 属性时,我已经让这段代码工作过。但是,当将此工厂直接注入我的控制器时,我得到的不是 response.data,而是 $$state 对象。
我的控制器如下所示:
app.controller('applicationController', ['$scope', 'objectArray', function($scope, objectArray) {
$scope.array = objectArray;
console.log($scope.array);
}]);
【问题讨论】:
-
@Kenney 我很好奇为什么它可以在没有任何额外代码的角度 ui-router 中工作,但在这种情况下有 {$$state:object}
-
我不确定,但您不能从
Promise.then返回值。你的方法类似于this吗?