如何在Angular promise请求中获取结果数据
How to get result data in Angular promise request?
我正在学习Angular,我的代码有问题。我正在尝试使用promise和service创建一个ajax请求。我在ajax访问中的输出是成功的,但我无法在视图中显示它。
这是我的一些代码:
<div ng-app="promiseCall">
<div ng-controller="promiseCtrl">
<button tyle="button" class="btn btn-default" ng-click="promiseCallRequest()">Promise Call</button>
<table class="table table-bordered table-hovered">
<thead>
<tr>
<td class="text-center"><label>ID</label></td>
<td class="text-center"><label>TITLE</label></td>
<td class="text-center"><label>BODY</label></td>
<td class="text-center"><label>CREATED AT</label></td>
<td class="text-center"><label>UPDATED AT</label></td>
</tr>
</thead>
<tbody>
<!-- no output -->
<tr ng-repeat="item in noteListing">
<td>{{ item.id }}</td>
<td>{{ item.title }}</td>
<td>{{ item.body }}</td>
<td>{{ item.created_at }}</td>
<td>{{ item.updated_at }}</td>
</tr>
</tbody>
</table>
</div>
</div>
angular.module('promiseCall',[])
.controller('promiseCtrl', function($scope, $log, noteData) {
$scope.promiseCallRequest = function() {
var getPromiseCallData = noteData.getNoteData();
getPromiseCallData.then({
function(payload) {
console.log(payload); //no output :(
//$scope.noteListing = payload.data;
},
function(errorPayload) {
$log.error('Failure request in note', errorPayload);
}
});
}
}).factory('noteData', function($http, $log, $q) {
return {
getNoteData: function() {
var deferred = $q.defer();
$http.get("<?php echo site_url('tutorial/getAjaxData'); ?>")
.success(function(data){
/*
deferred.resolve({
id: data.id,
title: data.title
});
*/
//console.log('success'); -- ok
})
.error(function(msg, code){
deferred.reject(msg);
$log.error(msg, code);
alert('there must be an error!');
});
return deferred.promise;
}
}
});
以下是JSON输出:
{"article_data":[{"id":"1","title":"sample updated 1","body":"sample 1","created_at":"2015-06-15 15:37
:28","updated_at":"2015-06-15 21:38:46"},{"id":"2","title":"sample 2","body":"sample 2","created_at"
:"2015-06-15 15:37:54","updated_at":"2015-06-15 15:37:54"}]}
尝试删除getPromiseCallData.then({});
中的{}
。
例如
$scope.promiseCallRequest = function() {
var getPromiseCallData = noteData.getNoteData();
getPromiseCallData.then(
function(payload) {
console.log(payload); //no output :(
//$scope.noteListing = payload.data;
},
function(errorPayload) {
$log.error('Failure request in note', errorPayload);
}
);
};
希望能有所帮助。
尝试以下代码:
....
getPromiseCallData.then(function(payload) {
console.log(payload); //no output :(
//$scope.noteListing = payload.data;
}).catch(function(errorPayload) {
$log.error('Failure request in note', errorPayload);
});
...
getNoteData: function() {
return $http.get("<?php echo site_url('tutorial/getAjaxData'); ?>");
}
在getNoteData
中,您所做的是一个promise反模式,您已经有了promise,不需要为它创建另一个包装器。
编辑:
如果你想记录服务的成功和错误,你可以简单地做,你仍然不需要额外的承诺:
getNoteData: function() {
return $http.get("<?php echo site_url('tutorial/getAjaxData'); ?>").then(function(data){
//some log
return data;
}, function(err){
// log err
return err;
});
}
相关文章:
- 对象拟合:获取结果尺寸
- Mongodb$in以与数组中相同的顺序获取结果
- 获取结果未定义(主干.js)
- 无法从IE中的data.responseText获取结果,但在其他浏览器中工作
- 从sqlite数据库获取结果
- 谷歌地图-从连接的结果[1].formated_address中获取结果[0].formated_address
- 如何使用 HtmlUnit 从 JavaScript html 页面获取结果
- JS:如何从其函数中获取结果值
- GET 请求,但未在视图中获取结果
- 如何查询具有特定 id 的 json 文件并获取结果数组
- 从成功的AJAX帖子中获取结果,并使用globalEval设置HTML
- 如何使用 Parse.promise javascript 定义一个承诺数组并获取结果数组 [parse.com]
- 通过 javascript 从 LinkedIn API 获取结果
- Ajax 从 sql 查询中获取结果
- 如何在“node-mysql”查询中获取结果的值
- 如何先从Firebase获取结果,然后重定向到另一个页面
- 使用javascript从struts.xml获取结果
- 如何从请求主体获取结果
- 从nodejs中的第二个函数中获取结果
- Angularjs:无法迭代或从服务结果中获取结果