material icon indicating copy to clipboard operation
material copied to clipboard

md-autocomplete to watch md-items

Open rscottfree opened this issue 11 years ago • 7 comments

I asked this question on the forums but after looking at the code I realize this is a new feature request.

I would like to have the md-autocomplete controller watch the array (if it is an array) in md-items for changes. When there is a change, I want it to update the list of suggestions. This allows me to update the list of suggestions without a promise. (In the md-search-text-change callback my directive controller dispatches an event to get the list of suggestions then it listens for an event to know they were updated.)

This watch behavior is the same implemented in the angular/ui-select component.

A quick (and error-laden) fix would be to add the following to the configureWatchers() method of MdAutocompleteCtrl. This simply watches for changes and calls the fetchResults() method.

$scope.$parent.$watchCollection(itemExpr, function() {
  fetchResults($scope.searchText);
});

rscottfree avatar Jul 15 '15 21:07 rscottfree

Could your issue be resolved by adding md-no-cache="true" to your md-autocomplete?

erinjanae avatar Oct 21 '15 22:10 erinjanae

Don't think so. I want it to actively watch the list of items; as far as I know setting this no-cache to true doesn't add any sort of watching.

rscottfree avatar Oct 21 '15 22:10 rscottfree

I would also like this functionality! Here is why: my md-autocomplete populates results from a local DB as well as via remote API call to Facebook. I want the results from local DB to be recomputed on each keystroke, but I ONLY want to fetch from Facebook when the user pauses typing. This means a refresh of md-items must be triggered by something other than search text changing.

jfhannel avatar Nov 08 '15 23:11 jfhannel

+1!

ThomasStubbe avatar Feb 26 '16 10:02 ThomasStubbe

Is there a workaround?

ThomasStubbe avatar Feb 26 '16 10:02 ThomasStubbe

@ThomasStubbe My workaround was to create a promise, hang on to it, and resolve it with the list of items once they were ready (wherever they happened to come from, be it an event, another promise, callback, etc.). In your case, however, this wouldn't work since the promise can only resolve once and it looks like you want to trigger it more than once when the text changes. If you figure this out, let me know :) In the meantime here is my workaround if it helps.

In my <md-autocomplete> I have md-items="item in ctrl.getItems()"

My getItems() looks something like:

getItems() {
        this.service.getItems(); // Somewhere in the service an event will fire when the items are ready.
        this.itemsDeferred = this.$q.defer(); // hang onto this and resolve when you get your items back
        return this.itemsDeferred.promise; // make md-autocomplete happy
}

Then when my event comes back:

this.$scope.$on('myevent', (event, items) => { this.itemsDeferred.resolve(items); }

rscottfree avatar Feb 26 '16 16:02 rscottfree

I have found if you quickly blur and refocus, it will refresh the choices

var input = angular.element("#my-autocomplete")
input.blur();
s.$applyAsync(() => {
    input.focus();
}, 1)

ghost avatar Jan 13 '17 17:01 ghost