Ionic Angular闭包用于尝试返回json数据
Ionic Angular Closure for trying to return json data
我有一个Ionic应用程序,我正试图从闭包内部返回数据。控制台正确显示了所有内容,但我似乎无法正确返回数据。我尝试了几种不同的变体,但没有成功。
$scope.callbackMethod = function (query) {
//var ritems= new Array();
var ritems;
Inventory.search(query, $scope.currentUser.token, $scope.currentUser.user_id, function(response) {
//console.log(JSON.stringify(response));
if(response.success == true)
{
$ionicLoading.hide();
$scope.requestmodal.hide();
console.log(response.items);
ritems= response.items;
}
else
{
$ionicLoading.hide();
//console.log(response.message);
return $scope.errorMessage = response.message;
}
});
console.log(ritems);
return ritems;
};
还有这个:
$scope.callbackMethod = function (query) {
//var ritems= new Array();
var ritems = Inventory.search(query, $scope.currentUser.token, $scope.currentUser.user_id, function(response) {
//console.log(JSON.stringify(response));
if(response.success == true)
{
$ionicLoading.hide();
$scope.requestmodal.hide();
console.log(response.items);
return response.items;
}
else
{
$ionicLoading.hide();
//console.log(response.message);
return $scope.errorMessage = response.message;
}
});
console.log(ritems);
return ritems;
};
返回到脚本的json是:
{"success":true,"items":[{"id":"1","0":"1","name":"Product 1","1":"Product 1","ref_id":"","2":""},{"id":"2","0":"2","name":"Product 2","1":"Product 2","ref_id":"","2":""}],"message":""}
有什么想法吗?非常感谢!!
您可以使用此方法来解决问题
var cbm = function (query) {
var defer = $q.defer();
Inventory.search(query, $scope.currentUser.token,$scope.currentUser.user_id, function(response) {
if(response.success == true)
{
$ionicLoading.hide();
$scope.requestmodal.hide();
defer.resolve(response.items) ;
}
else
{
$ionicLoading.hide();
defer.reject(response.message);
}
});
return defer.promise;
};
在呼叫时,您可以调用类似的回调方法
$scope.callbackMethod = function (query){
return cbm(query).then(function(data){ return data;});
}
相关文章:
- Jquery:对返回JSON数据的php脚本的Ajax调用
- 返回JSON中的下标
- jQuery Map 从多个源返回 JSON 对象
- 如何在 Node.js 中返回 JSON 后停止代码流
- AngularJS:表单提交没有返回JSON中的值
- 使用ajax发送数据,返回json
- 从我的解析函数返回json
- 如何从脚本页面返回json数据
- 有些人有任何关于如何使用Modified Java Script Value处理返回Json(url)的示例
- 如何在html页面中从ajax返回json数据
- Ajax调用在本地返回json,但在服务器上运行时返回xml
- ajax调用返回JSON ParseError即使认为JSON看起来是正确的
- 调用.ajax方法得到未定义的json结果,返回json格式列表<字符串>
- 如何通过C#返回JSON并用JavaScript进行解析
- 使用 AJAX 返回 json 数组
- 从 AJAX 命令返回 JSON 格式的响应
- 如何通过node中的请求返回JSON.js(从javascript)
- Node.js API 在终端中返回 JSON,但不在浏览器中返回 JSON
- 如何在支柱 2 中返回 JSON 数组
- Rhino:从 Java 中返回 JSON