无法显示HTTP GET中Angularjs应用从工厂响应控制器中 [英] unable to display the http get response from the factory in controller in Angularjs application
问题描述
在我的服务我做HTTP GET请求,如下图所示:
.factory('InvoicesGeneralService',函数($ HTTP){
返回{
getAgreementsByCourierId:功能(courierId){
的console.log(Courier在服务+ courierId);
返回$ http.get('/ API /协议/ byCourierId',{params:一个{courierId:courierId}}),然后(功能(响应){
返回响应;
});
}
};
});
而在浏览器控制台我看到以下响应:
[
{
ID:3,
数字:AGR53786
ediNumber:EDI7365,
的startDate:2012-08-30,
结束日期:2018年7月1日,
信使:{
ID:2,
名:联邦快递,
URL:www.fedex.com
isActive:真实,
isParcel:真
},
客户:{
ID:4,
code:KJGTR
名:酣畅
isActive:真实,
engageDate:2011-07-07,
isSendRemittance:真实,
upsUserName:TKD,
upsPassword:KUU,
isEligibleForTracking:真实,
isEligibleForAuditing:真实,
状态:5
}
}
]
在我的控制,我在分配给结果列表:
$ scope.resultList = InvoicesGeneralService.getAgreementsByCourierId(selCourierId);
但我的 resultList
总是显示为空。任何一个可以帮助我,为什么会这样呢?
当我想,如下图所示,显示 resultList
,它总是显示空对象, {}
。它应该显示来自服务的响应JSON数组,但它显示空对象。
< pre类=code> {{resultList | JSON}}< / pre>
$ HTTP
返回的承诺。任何消费数据需要处理它像一个承诺了。
InvoicesGeneralService.getAgreementsByCourierId(selCourierId)。然后(功能(数据){
$ scope.resultList =数据;
});
另外,你的工厂的然后
函数是不是做的时刻任何事情。你应该从中返回响应的数据。
返回$ http.get('/ API /协议/ byCourierId',{params:一个{courierId:courierId}}),然后(功能(响应){
返回response.data;
});
In my service I making http get request as shown below:
.factory('InvoicesGeneralService', function ($http) {
return {
getAgreementsByCourierId: function (courierId) {
console.log("Courier in Services" + courierId);
return $http.get('/api/agreements/byCourierId', {params: {courierId: courierId}}).then(function (response) {
return response;
});
}
};
});
And in browser console I am seeing the following response :
[
{
"id":3,
"number":"AGR53786",
"ediNumber":"EDI7365",
"startDate":"2012-09-02",
"endDate":"2018-07-01",
"courier":{
"id":2,
"name":"FedEx",
"url":"www.fedex.com",
"isActive":true,
"isParcel":true
},
"client":{
"id":4,
"code":"KJGTR",
"name":"Hearty",
"isActive":true,
"engageDate":"2011-07-07",
"isSendRemittance":true,
"upsUserName":"Tkd",
"upsPassword":"kuu",
"isEligibleForTracking":true,
"isEligibleForAuditing":true,
"status":5
}
}
]
And in my controller I am assigning it to result List :
$scope.resultList = InvoicesGeneralService.getAgreementsByCourierId(selCourierId);
But my resultList
is always appearing as Empty. Can any one help me, why it is happening?
When I am trying to display resultList
as shown below, it always shows empty object, {}
. It supposed to display the response json array from the service but it is showing empty object.
<pre class="code"> {{resultList | json}}</pre>
$http
returns a promise. Anything consuming that data needs to handle it like a promise too.
InvoicesGeneralService.getAgreementsByCourierId(selCourierId).then(function(data) {
$scope.resultList = data;
});
Also, your factory's then
function is not doing anything at the moment. You should return the response's data from it.
return $http.get('/api/agreements/byCourierId', {params: {courierId: courierId}}).then(function (response) {
return response.data;
});
这篇关于无法显示HTTP GET中Angularjs应用从工厂响应控制器中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!