javascript - AngularJS two $http function async return object -
this question has answer here:
- how return response asynchronous call? 21 answers
i using angularjs of $http function, have 1 async function need other 1 feedback variable , continue ,my code follows:
function getdatalist(company_id) { return $http.get("https://for_test_document/restful_api/brandsearch/format=json&company_id=" + company_id); } then try use follows, json has been received correctly:
for (var = 0; < len; i++) { getdatalist(json.data[i].id).then(function(successdata) { var len = successdata.data.length; var brand_name = ""; (var j = 0; j < len; j++) { brand_name = brand_name + successdata.data[j].brand_name + '/'; } console.log(brand_name); return brand_name + "" }) my question that, console.log have show brand_name correctly, why in browser shows object? 
my html follows:
function loadinfo(){ $http({ method:'get', url:'https://for_example/restful_api/companysearch/?format=json', datatype: "json", contenttype : 'application/json', accept: 'application/json', }).then(function (json){ get_company_type().then(function (data) { unique_company_type = data.data }) /*insert data table */ var len =json.data.length; var tablestr = ''; tablestr = tablestr+ content tablestr = tablestr+'<tbody role="alert" aria-live="polite" aria-relevant="all">'; for(var i=0;i<len;i++){ tablestr = tablestr+'<td class=" ">' + getdatalist(json.data[i].id).then(function (successdata){ var len = successdata.data.length; var brand_name = "" ; for(var j=0;j<len;j++){ brand_name = brand_name +successdata.data[j].brand_name+'/'; } console.log(brand_name); return brand_name + "" }) + '</td><td class=" ">' +json.data[i].city + '</td><td class=" ">' +json.data[i].address + '</td><td class=" ">' +'client contact' + '</td><td class=" ">' + 'contact details' + "</td></tr>"; } tablestr = tablestr+"</tbody>"; thanks helps.
clearly returns object of type brand_name, not sure it's supposed that. can try parse forcibly string using tostring() :
for (var = 0; < len; i++) { getdatalist(json.data[i].id).then(function(successdata) { var len = successdata.data.length; var brand_name = ""; (var j = 0; j < len; j++) { brand_name = brand_name + successdata.data[j].brand_name.tostring() + '/'; } console.log(brand_name); return brand_name + "" })
Comments
Post a Comment