我是AngularJs世界的新手 - 我试图从两个REST WS调用中获取数据.
第一个返回一组数据(工作正常) - 使用第一个数据的值,我需要进行另一个webservice调用并检索数据并在表中打印.
以下是我到目前为止所尝试的内容: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>
Run Code Online (Sandbox Code Playgroud)
脚本:
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"
}];
});
}
}
]);
Run Code Online (Sandbox Code Playgroud)
任何帮助很多appreaciated.
我会这样做,第二次调用成功响应.
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
};
Run Code Online (Sandbox Code Playgroud)
关于您的代码,几乎没有什么可以注意到的:
避免.success和.error使用.then(function(success){}, function(error){});.大多数框架都弃用了这两个第一,并且角度文档根本不再谈论它.
https://docs.angularjs.org/api/ng/service/ $ q
避免将所有内容放在$ scope中.只需将需要与视图或其他控制器共享的内容放在一起.
尽快,了解共享功能的服务,并让1层应用程序负责一件事
小智 1
您的调用是异步进行的,因此在进行调用时,其余代码仍在执行。因此,
<p ng-repeat="item in fetchRequest(request.price)"></p>
Run Code Online (Sandbox Code Playgroud)
甚至在定义之前执行request.price。
您需要做的是将调用链接到您的 api 调用:
<p ng-repeat="item in fetchRequest(request.price)"></p>
Run Code Online (Sandbox Code Playgroud)
然后更改:
<p ng-repeat="item in fetchRequest(request.price)"></p>
Run Code Online (Sandbox Code Playgroud)
到:
<p ng-repeat="item in request.items"></p>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
22290 次 |
| 最近记录: |