1
我正在使用Angular的项目之一的搜索控件。我正在尝试延迟响应,例如搜索框应该等待2秒才能调用相关函数。使用Angular进行延迟搜索
当我在网上搜索时,大部分建议都是使用指令。我不能使用指令,因为点击搜索结果依次显示另一组结果。在网络上沉思之后,我得到了另一个解决方案。
我的代码:
<div ng-controller="ReportCtrl" ng-app="pReport">
<input type="text" ng-model="searchString" placeholder="Search"/>
<div ng-show="display == 'search'">
<ul>
<li ng-repeat="i in batches">
<a ng-click='selectBatch(i.batchnumber)'>{{i.batchnumber}}</a>
</li>
</ul>
</div>
<div ng-show="display == 'repdetail'">
<ul>
<li ng-repeat="i in reports">
<a href='{{i.filename}}'>{{i.screenname}}</a>
</li>
</ul>
</div>
</div>
角表达式
var app = angular.module('pReport', []);
app.controller('ReportCtrl', function ($scope, $http, $timeout) {
var timer = false;
$scope.batches = [];
$scope.reports = [];
$scope.$watch('searchString', function() {
if (timer) {
$timeout.cancel(timer);
}
timer = $timeout(function() {
$http({ }). //POSTING DETAILS
success(function (data, status, headers, config) {}); //SEARCH RESULTS
}, 1000)
});
$scope.selectBatch = function (batchNumber) { } // SEARCH FURTHER
});
- 这有什么错在拖延的这种做法?
- 我应该使用指令吗?
- 有没有其他最有效的方法?
在此先感谢您的帮助!
更好的方法是使用一个指令。既然你对这种方法不感兴趣,那就没问题了。这种方法没有错。 – BKM