In my angularjs app http://1ffa3ba638.url-de-test.ws/zombieReport/popup.html#/lord i try to make a live instant search : i want nothing show at starting and search start when two letters minimum is writed. And after the searching is again performed for 3 or more letters, new query for each new letter.
/* INIT .myForm */
$scope.myForm_lord = {};
$scope.posts = {};
/* AJAX POST QUERY : calling lord watching */
$scope.$watch('myFormZR_lord.$valid', function() {
// ng-show things
$scope.successLordZR = true;
// RETRIEVE DATAS
var dataName = $scope.myForm_lord.search;
// CONSOLE LOG CONTROL
console.log(defineCLC + "LORD search requested by name : " + dataName);
// $resource
var Post = $resource(
urlLordRest,
{name: dataName},
{'query': {method: 'GET', isArray: true, cache: false, headers: {'Accept': 'application/json'}}}
);
$scope.posts = Post.query();
console.log($scope.posts);
});
html:
<form name="myFormZR_lord" id="myFormZR_lord" class="form-horizontal" role="form" novalidate="">
<div class="form-inline form-inline-sm">
<!-- form search -->
<div class="input-group">
<input class="form-control" name="search" type="text" ng-required="true" minlength="2" ng-minlength="2" ng-model="myForm_lord.search" placeholder="{{ 'TRS_CTRL3_FORM1' | translate }}" autocomplete="off" />
</div>
</div>
<span class="myForm_error" ng-show="myFormZR_lord.$invalid">{{ 'TRS_CTRL3_FORM2' | translate }}</span>
</form>
<div ng-show="successLordZR">
<p>{{ 'TRS_CTRL3_TEXT1' | translate }} :</p>
<ul>
<li ng-repeat="post in posts">
<p>{{post.prefixe}} {{post.name}}</p>
</li>
</ul>
</div>
Problem is actually results are showing at starting, and there is only a query for two letters, not if we put 3 or more. And the query is executed two times (see console log), what's wrong ? i use $watch, it's not good ?