我正在使用查询过滤数组(results)。通过删除过滤器,结果会准确显示所有元素。通过删除更新结果的 http.get并初始化结果以包含元素,过滤器可以准确过滤。
使用过滤器和 http.get 更新结果,即使结果包含数据,也会显示没有结果的消息。我已经验证http.get 正确更新了结果(如下所示{{results}}正确显示)。以下是{{results}}显示的内容:
[
{
"name": "Company Number 1 ",
"description": "description of 1 "
},
{
"name": "Company Number 1 ",
"description": "description of 1 "
},
{
"name": "Company Number 2 ",
"description": "description of 2 "
}
]
索引.html
<div class="form-group label-floating">
<label class="control-label" for="addon1">Filter</label>
<input id="addon1" class="form-control" type="text" data-ng-model="query">
</div>
{{results}} <!--This works properly-->
<table class="table table-striped" id="resultsTable">
<tbody>
<tr data-ng-show="resultsFilter.length === 0">
<td class="center" colspan="3">No results found.</td> <!--This message appears even when results should have elements-->
</tr>
<tr data-ng-repeat="result in resultsFilter = (results | filter:query | limitTo:displayNum) track by result.name">
<td>
<button class="btn btn-primary btn-block">
{{result.name}}
</button>
</td>
</tr>
</tbody>
</table>
角.js
$scope.query = "";
$scope.displayNum = 20;
$scope.results = [];
...
$scope.updateResults = function () {
$http({
method: "GET",
url: requestUrl,
params: {id: $scope.identifierInHierarchy.companyId}
})
.then(function successCallback(response) {
$scope.query = "";
$scope.results = [];
response.data.forEach(function (entry) {
$scope.results.push({
name: "Company Number " + entry.CompanyNumber,
description: "description of " + entry.CompanyNumber
});
});
},
function errorCallback(response) {
});
}
更新:不,将 resultsFilter 更改为 results 并不能解决问题。我也相信这是不正确的,因为保持 resultsFilter 和静态初始化结果将正确过滤(如前所述)。