angularjs - Form watch with debounce fires multiple ng-submit events when hitting enter -
i need watch form input changes , fire search. search contains number of filters searching(not shown in plnkr). i'm watching form object accomplish this. input debounced not make repeated requests. problem have when user types search value , hits enter submit() function fires twice, 1 watch , 1 enter key ng-submit. how refactor this? need user able input text in search field , have search trigger after debounce interval, if input text , hit enter should ignore watch. can think of may different ways try fix this, hoping preferred solution 1 of :) thanks!
see this: https://plnkr.co/edit/ls5cqzs9x4ngi745po7c?p=preview
<body ng-app="submitexample"> <script> angular.module('submitexample', []) .controller('examplecontroller', ['$scope', function($scope) { $scope.list = []; $scope.text = 'hello'; $scope.$watch('text', function(newvalue, oldvalue) { if(newvalue != oldvalue) { $scope.submit(); } }); $scope.submit = function() { if ($scope.text) { $scope.list.push(this.text); } }; }]); </script> <form ng-submit="submit()" ng-controller="examplecontroller"> enter text , hit enter: <input type="text" ng-model="text" ng-model-options="{ debounce: 1000 }" name="text" /> <input type="submit" id="submit" value="submit" /> <pre>list={{list}}</pre> </form> </body>
Comments
Post a Comment