我正在尝试从我的 AngularJs 服务返回一个值到我的 Controller ,无论我在 Controller 内部传递它。它确实传递对象并将数据记录在控制台中。这是我的代码:
angular.module('demoService',[])
.factory('myService',
function($http) {
return {
getAll: function (items) {
var path = "http://enlytica.com/RSLivee/rest/census"
$http.get(path).success(function (items) {
console.log(items.Tweets[1].FAVOURITE_COUNT);
});
}
}
});
我如何在函数调用时返回“items.Tweets[1].FAVOURITE_COUNT”。
提前致谢
最佳答案
您可以创建查询数据的 promise ,通过使用方法 then
,getAll
将在 API 响应之前知道要做什么。
angular.module('demoService')
.factory('myService', ['$http',
function ($http) {
function getAll (items) {
var path = "http://enlytica.com/RSLivee/rest/census"
return $http.get(path).then(
function (res) {
return res;
});
}
return {
getAll: getAll;
}
}
}];
关于jquery - AngularJS 服务的返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30411809/