javascript - how to configure autocomplete search box to delete initial characters and not call the service -
my project requirement create autocomplete search box fetches data elastic search . make simple have textbox on onchange() event fires autocomplete webservice , fetches data. using angularjs 1.6 .
<input type="text" ng-change="fetchtags ()" ng-model="autocompletedata.text"/>
javascript
$this.fetchtags = function() { try { //remove special chars/ $this.autocompletedata.text = purgerfilter($this.autocompletedata.text); //cancel previous timer; if (autocompletetimer) { $timeout.cancel(autocompletetimer); } //to avoid late response display suggesion list $this.autocompletedata.hasemitted = false; var txt = $this.autocompletedata.gettext(); if (typeof txt !== undefined) { //200ms debounce. var time = 200; if (txt.length > 0) { autocompletetimer = $timeout(function() { $this.autocompletedata.newtag = undefined; initiategettopictagsfromwebservice(); }, time); } else { $this.autocompletedata.setisvisible(false); } } } catch (e) { throw e; } };
everything working fine. dont go other function calls. problem in function.
so here whats happening : 1.if start typing works , actucal , proper response per keywords.
2.if delete . i,e last character first working fine.
3.the problem case : if had remove initial characters . eg textbox word java script. , decide remove ending java . service called "jav script" dont want. case should not case change function fire.
this configuration want in autocomplete search textbox.
did expect this.. ng-change
not pass $event
variable.
angular.module("myapp", []) .controller("mycontroller", function($scope) { $scope.checkword = ""; $scope.mydata = function(event) { // console.log(event); if(event.keycode == 8 || event.keycode == 46){ // console.log(event); if(event.srcelement.selectionend == $scope.checkword.length || event.srcelement.selectionstart == $scope.checkword.length){ $scope.msg="i'm going call function "; }else{ $scope.msg="i'm not going call function "; } } } } );
<html><head><script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script></head> <body ng-app="myapp"> <div ng-controller="mycontroller" > <input type="text" ng-model="checkword" ng-keydown="mydata($event)"/> <br><br>{{msg}} </div> </body> </html>
Comments
Post a Comment