In my app I had simple angular.js filter and it worked fine, but now I need to integrate server-side search. I have endpoint for this and I created directive which watches query in input and makes request to server returning results:
html:
<search ng-model="query"></search>
js:
...
restrict: 'E',
scope: {
ngModel: '='
},
template: '<input type="text" ng-model="ngModel" />',
link: function (scope, elem, attrs) {
var timer = false;
scope.$watch('ngModel', function (value) {
if (timer) {
$timeout.cancel(timer);
}
timer = $timeout(function () {
if (value) {
scope.$parent.items = rest.query({ resource: 'search', query: value });
}
}, 1000);
});
}
...
However the problem is in scope. As you see I'm writing results to parent scope items because I need the search results stay on the same page with same controller (as it was like in client-side filter):
common template for several controllers and search results:
<ul class="items">
<li class="item item{{$index+1}}" ng-repeat="item in items">
...
</li>
</ul>
So after representing results of server-side search query, when clearing input field I need somehow to return all items that were represented before search and currently cannot find optimal solution for this..
Maybe someone made something similar before?