我难以理解ng-repeat为什么不显示搜索结果。什么适用:角度1.6纳克重复不显示搜索结果
进行HTTP GET请求以从数据库中获取所有数据,并且ng-repeat显示结果。
制作使用
searchterm
来从数据库中的数据的一部分,和NG-重复的HTTP GET请求显示结果,与$scope.searchterm = "acme";
在控制器设定searchterm
。我在模板中的搜索框将
searchterm
发送到控制器,我可以在console.log中看到它,然后HTTP GET请求会熄灭,并返回正确的数据,我也可以在控制台中看到日志。
不起作用的是使用ng-repeat在模板中显示搜索结果数据。
这里是我的HTML模板:
<div ng-controller="HomeController">
<form>
<label>Search: <input type="text" ng-model="searchterm" /></label><br />
<input type="submit" ng-click="search(searchterm)" value="Search" />
</form>
</div>
<div ng-repeat="network_operator in network_operators">
<span>{{network_operator.name}}</span><br />
</div>
这里是我的控制器:
app.controller('HomeController', ['$scope', '$http', function($scope, $http){
console.log("Home controller.");
// This code displays all of the data, using ng-repeat in the template
// $http.get('http://qualifynow.herokuapp.com/products?searchstring=').then(function(response) {
// $scope.network_operators = response.data.products;
// console.log($scope.network_operators);
// }, function(response) {
// console.log("Error, no data returned.");
// });
// This works instead in place of the next line, the search results display with ng-repeat
// $scope.searchterm = "acme";
$scope.search = function(searchterm) { // This is the line that kills the ng-repeat
console.log($scope.searchterm); // This works, the search term is passed to the controller
$http.get('http://qualifynow.herokuapp.com/products?searchstring=supplier:' + $scope.searchterm).then(function(response) {
$scope.network_operators = response.data.products;
console.log($scope.network_operators); // The correct search results are logged
console.log($scope.network_operators[0].name); // The correct name is logged
}, function(response) {
console.log("Error, no data returned.");
});
};
}]);
我试图运行$scope.apply()
,但是这导致的错误消息$digest
已经运行。
是$scope.search
功能创建的新$scope
?也就是说,当ng-repeat
试图在旧的$scope
中查找数据时,我的数据是否愉快地位于新的$scope
?
我试过ng-submit
而不是ng-click
,结果是一样的。
我试着制作一个完美的过滤器,但是对于大型数据库,将所有数据放在$scope
然后过滤以获得搜索结果是没有意义的。仅获取用户想要的数据会更快。
用$超时结束$ scope.apply(),你不会得到摘要错误 – GMaiolo