Fill <input /> with autocomplete:selected
How would I fill the input with the selected value?
This worked but has a huge lag:
$scope.$on('autocomplete:selected', function(event, suggestion, dataset) {
console.log(suggestion, dataset);
$scope.q = suggestion.name
});
Did you try https://github.com/algolia/autocomplete.js#jqueryautocompleteval-val?
Thanks @legshooter - Do you know if there is a way to do this without jQuery? (Using Angular)
Javascript:
console.log(document.getElementById("search-input").autocomplete('val'))
HTML:
<input id="search-input" ng-model="q" autocomplete aa-datasets="getDatasets()" />
// LOGS: document.getElementById(...).autocomplete is not a function
It makes sense, it is a jQuery plugin, it literally doesn't exist where you're trying to call it. I have no Angular experience atm, so I can't help you with that, though I thought Angular excels at data binding. Maybe someone else has an idea, or one of the maintainers catches up with this.
Adding $scope.$digest() helped speed up the process, but when tabbing/blurring out of the input, the value changes to its pre 'autocomplete:selected' value.
$scope.$on('autocomplete:selected', function(event, suggestion, dataset) {
console.log(suggestion, dataset);
$scope.q = suggestion.name
$scope.$digest()
});
Yes I think calling $digest or $apply is the way to go.
Then, about the value being changed to its previous value; I'm not sure to understand why. Maybe setting the $scope.q is not enough and we should call the underlying setVal method as well.
Maybe try
$scope.$on('autocomplete:selected', function(event, suggestion, dataset) {
$scope.$apply(function() {
$scope.q = suggestion.name;
document.getElementById("search-input").autocomplete('val', suggestion.name);
});
});
PS: the provided angular binding is just a wrapper on top of the jQuery library, that's why it's not 100% angular-proof.
I had a strange reaction where the ng-model was updated properly (I could log it in a function), but for some reason the input would stay the short version. Seemed impossible...
Thanks for the help on this, but I had to resort to another autocomplete library. Still using Algolia for data retrieval :)
I was having a similar issue, I was able to pull off what I wanted like this...
var searchInput = angular.element("#label");
$scope.q = target.label;
$timeout(function() {
searchInput.val($scope.q).focus();
}, 0);
I figured that out, but only using jquery directly. Because none of the options above worked for me. Turns out, that this library uses an interval variable to keep the value. So even if you set the input directly with jquery, with $('#selector').val('some-value') it'll return back on blur.
I discovered that this internal variable was set in the onInput event. So what I did was only trigger this function, here's the final code:
$scope.$on('autocomplete:selected', function(event, suggestion, dataset) {
let element = angular.element('#selector');
element.val($scope.q).trigger('input').focus();
});
I would be nice if the directive would return the element in the $emit function, so you can look which input executed the search request.
We have a use case where we have 2 inputs with the autocomplete directive, but each one of them has a different business logic to it. The $scope.$on('autocomplete:selected', function(event, suggestion, dataset) does not allow for dynamic selectors.
It whould be nice to have something like this.
$scope.$on('autocomplete:selected', function(event, suggestion, dataset, element) {
let element = angular.element(`#${element.name}`);
element.val($scope.q).trigger('input').focus();
});
UPDATE
I created a simple directive
<input
type="text"
id="input-edit"
name="input-edit"
ng-model="something"
autocomplete
aa-options="{autoselect:true}"
aa-datasets="Algolia.Search()"
user-search>
ANGULAR DIRECTIVE
return {
restrict: 'A',
controller: 'SearchUserController as user',
link: function (scope, element, attrs, controller){
var inputElement = element[0];
controller.setInputId(inputElement.id);
}
}
ANGULAR DIRECTIVE CONTROLLER
self.inputId = '';
self.setInputId = (value) => self.inputId = value;
$scope.$on('autocomplete:selected', function(event, suggestion, dataset) {
let optionSelected = suggestion.username;
let inputElement = angular.element(`#${self.inputId}`);
inputElement && inputElement.is(':visible') && inputElement.is(':focus') && inputElement.val(optionSelected).trigger('input').focus();
});
@a-covar We accept PR fixing this, that's a nice idea and should be backward compatible.
Quick note, isn't this in the event already bound to the correct element?