尝试从工厂返回数据并在工厂输出中记录正确的数据,但是一旦传递给控制器,它始终未定义。如果我在控制器内部有我的工厂逻辑,它将正常工作。所以它一定是简单的我在这里失踪?将工厂数据返回给控制器始终未定义
应用
var app = angular.module('app', []);
app.controller('animalController', ['$log', '$scope', 'animalResource', function($log, $scope, animalResource) {
$scope.list = function() {
$scope.list = 'List Animals';
$scope.animals = animalResource.get(); // returns undefined data
$log.info($scope.animals);
};
$scope.show = function() {};
$scope.create = function() {};
$scope.update = function() {};
$scope.destroy = function() {};
}]);
app.factory('animalResource', ['$http', '$log', function($http, $log) {
return {
get: function() {
$http({method: 'GET', url: '/clusters/xhrGetAnimals'}).
success(function(data, status, headers, config) {
//$log.info(data, status, headers, config); // return correct data
return data;
}).
error(function(data, status, headers, config) {
$log.info(data, status, headers, config);
});
},
post: function() {},
put: function() {},
delete: function() {}
};
}]);
日志信息
[Object, Object, Object, Object, Object, Object, Object, Object, Object, Object]
200 function (name) {
if (!headersObj) headersObj = parseHeaders(headers);
if (name) {
return headersObj[lowercase(name)] || null;
}
return headersObj;
} Object {method: "GET", url: "/clusters/xhrGetAnimals"}
'返回数据;'成功随叫随到返回不从'get'函数返回任何东西。尝试'返回$ http({method ...'。通过角度文档网站上的教程 – charlietfl
好吧,你是正确的,在$ http之前添加一个返回。让它成为答案,我接受它。谢谢 – dynamo