[英]using debounce on checkboxes angular
I am dealing with multiple checkboxes used to filter a set of data. 我正在处理用于过滤一组数据的多个复选框。 however I do not want the checkboxes to trigger a filter after every single click of a checkbox so I wanted to debounce it. 但是我不希望复选框在每次单击复选框后触发过滤器,所以我想对其进行反跳。 perhaps wait 500ms to a second after the last checkbox has been selected. 选中最后一个复选框后,可能要等待500毫秒至一秒。
check out the my plnkr 看看我的plnkr
<input type="checkbox"
ng-model="user.cool"
ng-model-options="{ debounce: 1000 }"/>
<input type="checkbox"
ng-model="user.lame"
ng-model-options="{ debounce: 1000 }"/>
here it basically just queues the checkbox click options and changes the model seconds apart but I want it to change both at the SAME time. 在这里,它基本上只是将复选框单击选项排队,并分开间隔秒数,但我希望它在同一时间都改变。 How can I accomplish this? 我该怎么做?
Thanks! 谢谢!
You may use $scope.$watch
in your app controller to control over result of multiple model properties changes. 您可以在应用程序控制器中使用$scope.$watch
来控制多个模型属性更改的结果。 $watch
can evaluate not only single property but also an expression; $watch
不仅可以评估单个属性,还可以评估表达式; then callback can be debounced; 然后可以取消回调。 later notify angular about the changes needed via calling $scope.$apply
. 稍后通过调用$scope.$apply
通知angular有关所需的更改。
$scope.$watch('user.lame + user.cool', _.debounce(function (id) {
$scope.$apply(function() {
//code that updates UI
})
}, 1000));
Also check this post: Can I debounce or throttle a watched <input> in AngularJS using _lodash? 还要检查一下这篇文章:是否可以使用_lodash在AngularJS中对受监视的<input>进行反跳或限制?
Here is the updated plunk: https://plnkr.co/edit/AkhEjEg8JCyM32j21MRQ?p=preview 这是更新的插件: https ://plnkr.co/edit/AkhEjEg8JCyM32j21MRQ ? p = preview
You could just angular's $watchCollection method, it'll do the same. 您可以只是angular的$ watchCollection方法,它会做同样的事情。 No extras required. 无需额外费用。
(function(angular) {
'use strict';
angular.module('optionsExample', [])
.controller('ExampleController', ['$scope',
function($scope) {
$scope.user = {
name: 'Igor'
};
$scope.$watchCollection('user', function(n, o) {
console.log(n);
});
}
]);
})(window.angular);
More concise form, one debounce definition and your form would look like this : 更简洁的表格,一个反跳定义,您的表格将如下所示:
<form name="userForm" ng-model="user" ng-model-options="{ debounce: 1000 }">
<label>Name:
<input type="text" name="userName" ng-model="user.name" />
<input type="checkbox" ng-model="user.cool" />
<input type="checkbox" ng-model="user.lame" />
</label>
<button ng-click="userForm.userName.$rollbackViewValue(); user.name=''">Clear</button>
<br />
</form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.