我是 AngularJs 世界的新手 - 我正在尝试从两个 REST WS 调用中获取数据。
第一个返回一组数据(工作正常)- 使用第一个数据的值我需要进行另一个网络服务调用并检索数据并打印在表格中。
以下是我目前尝试过的: HTML:
<table ng-table="tableParams" id="request_table" class="table table-striped table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
<th>Number</th>
<th>Price</th>
<th>Short Description</th>
<th>Requested for</th>
<th>State</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="request in req_list | orderBy:sortType:sortReverse | filter: searchItem">
<p ng-repeat="item in fetchRequest(request.price)"></p>
<td>{{request.number }}</td>
<td>{{request.price}}</td>
<td>{{request.short_description}}</td>
<td>{{request.requested_for.display_value}}</td>
<td>{{request.stage}}</td>
</tr>
</tbody>
脚本:
angular.module('sc_request_list')
.controller('MyRequestItems', [
'$scope',
'$http',
function($scope, $http) {
$scope.sortType = 'number' //set the default sort type
$scope.sortReverse = false; // set the default sort order
$scope.searchItem // set the default search/filter term
$scope.itemUrl = '/api/sc_request';
$scope.reqUrl = '/api/sc_req_item';
$http.defaults.headers.common.Accept = "application/json";
$scope.fetchRequestItemList = function() {
$http({
method: 'GET',
url: $scope.itemUrl,
}).
success(function(data, status) {
var result = data;
var json = JSON.stringify(result);
var data = JSON.parse(json);
$scope.req_list = data.result; // response data
}).
error(function(data, status) {
$scope.req_list = [{
"req_list": "Error fetching list"
}];
});
}
$scope.fetchRequest = function(request) {
console.log("Request Number is: " + request);
$http({
method: 'GET',
url: $scope.reqUrl + "/" + request,
}).
success(function(data, status) {
var result = data;
var json = JSON.stringify(result);
var data = JSON.parse(json);
$scope.req = data.result; // response data
}).
error(function(data, status) {
$scope.req = [{
"req": "Error fetching list"
}];
});
}
}
]);
非常感谢任何帮助。
最佳答案
我会这样做,在成功响应中进行第二次调用。
function getFirstItem(){ // Here I declare a function that will return a promise.
return $http({
method: 'GET',
url: $scope.itemUrl
});
}
function getDependantItem(){ // I declare a second function that returns a promise
return $http({
method: 'GET',
url: $scope.otherUrl
});
}
$scope.fetchRequest = function(request) { // Here is a function that can be called by the view.
getFirstItem()
/*
* I call the first function that returns a promise.
* the "then" is just setting a
* callback that will be executed when your server will respond.
* The next thing the code does, after registering the callback,
* is going to the end of your function,
* and return nothing.
*/
.then(function(result1){
$scope.req = result1.result; // Now that the server has answered, you can assign the value to $scope.req, and then, call the second function.
getDependantItem().then(function(result2){
// This is still an async call. The code keeps on running, and
// you will only have this callback running when your server will have responded
// Handle your response here and assign your response to a $scope variable.
}, function(error2){
// If an error happened during the second call, handle it here.
});
}, function(error1){
// If an error happened during first call, handle it here.
});
// If you want to return something, you must do it here.
// Before your promises would ever be resolved
// That's the reason why you got an undefined
};
有关您的代码的一些注意事项:
避免
.success
和.error
,使用.then(function(success){}, function(error){});
。大多数框架都弃用了这 2 个第一,并且 Angular 文档根本不再谈论它。
https://docs.angularjs.org/api/ng/service/ $q避免将所有内容都放在 $scope 中。只需将需要与 View 或其他 Controller 共享的东西放在一起即可。
尽快了解共享功能的服务,并让您的应用程序的一层负责一件事
关于Angularjs - 多个 $http REST 调用(第二个调用取决于第一个的输出),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32144898/