我有一个搜索字段。我想在一定延迟(例如2000毫秒)之后发送搜索查询
这是我设法生成的相关angularjs代码。
但我确信必须有更好的模式才能做到这一点......
function SearchController($scope, $http, $timeout){ var update_results = function(originalWord){ if (originalWord == $scope.search_term){ $http.get("/search/"+ $scope.search_term).success(function(data){ $scope.search_results = data; }); } }; $scope.search = function(){ var originalWord = $scope.search_term.replace("xxxx", "xxxx"); //UGLY HACK TO CLONE A STRING $timeout(function(){ update_results(originalWord); }, 2000); }; }
答案 0 :(得分:0)
你使用$ timeout的事实很棒。 我见过很多angularJS代码,并且所有代码都使用相同的机制在一定时间后启动事件。