无法在 javascript 中使用 promise.push 附加数据
Unable to append the data with promise.push in javascript
我想在各自的表格中显示该设置中的所有设置和员工数量。所以我想进行两个不同的http(Get)调用并附加这些结果。所以我的一个朋友建议我去承诺。我尝试了不同的方式,但我失败了。
<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);
});
}
]);
我面临的主要问题是"我想要亲子关系。在使用该 result.id 进行第一次http调用成功后,我们希望进行第二次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;
};
}
]);
当您有许多并行承诺时,需要使用此策略,并且希望在成功解析所有承诺时进行回调。
你可能想要什么,它被称为承诺链,它用前一个的答案调用下一个承诺。例:
travelService
.getDeparture( user ) // Request #1
.then( function( departure )
{
$scope.departure = departure; // Response Handler #1
return travelService.getFlight( departure.flightID ); // Request #2
})
.then( function( flight )
{
$scope.flight = flight; // Response Handler #2
return weatherService.getForecast( $scope.departure.date ); // Request #3
})
.then( function( weather )
{
$scope.weather = weather; // Response Handler #3
});
这是您可能想要查看的承诺链接指南的示例: http://solutionoptimist.com/2013/12/27/javascript-promise-chains-2/
相关文章:
- 使用promise和mongoose对文档进行排序
- 测试Angular Service解决错误回调中的promise
- 节点协同与生成器和Promise并行流量控制
- 根据是否解析了 Promise 从函数返回值
- NodeJS API调用中Array中的Push和Pull元素
- 将一个方法转换为promise:Nodejs
- 函数在promise被解析后被调用,但Jasmine未通过测试.为什么?
- js promise没有正确关闭
- Angularjs使用“;这个“;promise内的关键字回调
- AngularJS和promise值在调用本地函数时的效果-未定义
- 在ES6 Promise中,我应该在解决/拒绝之前使用return吗
- 以同步方式获取Javascript Promise的值
- NodeJS和pg promise,捕获PostgreSQL异常
- AngularJS使用http进行promise处理
- Angular promise return"未定义的“;值.NET MVC
- Promise函数在.then之后未运行函数化
- 如何为javascript promise进行阻塞并返回解析结果
- 将其中一个异步方法重写为使用promise的方法
- 无法在 javascript 中使用 promise.push 附加数据
- 为什么$scope.someArray.Push not一个有效的函数传递给$q.promise上的.then