【发布时间】:2013-12-12 17:22:59
【问题描述】:
我遇到了一个奇怪的问题:我的搜索表单向我的服务器发起了三个不同的请求,但是当 Angular 必须显示数据时,数据通常不会出现。我确定数据来自服务器,因为我在 FireBug 中看到了它们。所有请求都已发送......但我认为并非所有请求都由 angularJS 处理。也许我对它的使用有误。
这是我的代码:
$scope.queryUser = {};
$scope.index = ['central', 'distributed', 'other'];
//$scope.index = ['distributed'];
$scope.results = {};
$scope.url = "/MyApp/search.do?index=" ;
$scope.search = function(query) {
$scope.queryUser= angular.copy(query);
// Create the http post request
// the data holds the keywords
// The request is a JSON request.
for( i in $scope.index ){
$http.get($scope.url + $scope.index[i] + "&query=" + $scope.queryUser.value).
success(function(data, status, headers, config) {
$scope.status = status;
$scope.results[$scope.index[i]+"Items"] = data;
console.log(i);
for(x in $scope.results.distributedItems){
console.log(x);
}
})
.
error(function(data, status) {
$scope.data = data || "Request failed";
$scope.status = status;
console.log("error: " + $scope.status);
});
}
确实,console.log 只打印一次索引……这对我来说很奇怪。我认为这三个请求是重叠的。
然后是ng-repeat的代码
<div id='aggregated_results_domain'>
<div id="results_distributed">
<table id="table_results_distributed" cellspacing="5" cellpadding="3" bgcolor="#eee">
<tr ng-repeat="(keyDistributed, valueDistributed) in results.distributedItems">
<td>{{keyDistributed}}</td>
</tr>
</table>
</div>
<div id="results_central">
<table id="table_results_central" cellspacing="5" cellpadding="3" bgcolor="#eee">
<tr ng-repeat="(keyCentral, valueCentral) in results.centralItems">
<td>{{keyCentral}}</td>
</tr>
</table>
</div>
<div id="results_other">
<table id="table_results_other" cellspacing="5" cellpadding="3" bgcolor="#eee">
<tr ng-repeat="(keyOther, valueOther) in results.otherItems">
<td>{{keyOther}}</td>
</tr>
</table>
</div>
有什么建议吗?
提前致谢
【问题讨论】:
标签: javascript angularjs angularjs-ng-repeat