0
我想在各自的表中显示该设置中的所有设置和雇员人数。因此,我想创建两个不同的http(Get)调用并追加这些结果。所以我的一个朋友建议我去promise.push。我尝试了不同的方式,但我失败了。无法在javascript中添加promise.push数据
<html>
<table class="table">
<tbody>
<tr ng-repeat="setting in createAttendance.AtSettings">
<td>
<div class="col-lg-12">
<p class="leavesettingpara">
<strong><span class="parastrong"></span>{{setting.name}}</strong>
</p>
</div>
<div class="col-lg-12">
<div class="col-lg-3">
<p class="leavesettingpara">{{settings.numOfEmployees}} Employees</p>
</div>
<div class="col-lg-3">
<p class="leavesettingpara">{{setting.date | date:'medium'}}</p>
</div>
<div class="col-lg-3">
<p class="leavesettingpara">By {{setting.createdName}}</p>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</html>
控制器:
angular.module('myApp')
.controller('settingsDisplayController', ['$state', 'attendanceSettingsService'
function($state, attendanceSettingsService) {
attendanceSettingsService.getSettings().then(function(data) {
vm.AtSettings = data;
console.log("organization" + data);
});
}
]);
我面临的主要问题是:“我想与结果第一HTTP调用的父子relation.On成功.id我们想要进行第二次http呼叫“
服务:
angular.module('myApp')
.service('attendanceSettingsService', ['$q', '$http', '$state',
function($q, $http, $state) {
this.getSettings = function() {
var d = $q.defer();
$http.get('api/organizationName').success(function(result) {
var promises = [];
var defer = $q.defer();
var res = [];
promises.push($http.get('api/organizations/' + result.id + '/attendanceProfileSettings').success(function(res) {
console.log(res);
}));
console.log(res);
promises.push($http.get('api/setting/' + res.id + '/employees'));
$q.all(promises).then(function(data, status, headers, config) {
console.log(data);
d.resolve(data);
}, function(data, status, headers, config) {
console.log(" failure...." + status);
});
return defer.promise;
});
return d.promise;
};
}
]);
你可能想使用承诺链接。请查看http://www.html5rocks.com/en/tutorials/es6/promises/#toc-promises-queues – jbrown