Note that there are some explanatory texts on larger screens.

plurals
  1. PORefresh the typeahead content with angularjs
    primarykey
    data
    text
    <p>The situation I have is the following:</p> <p>I have a search field where the user types a text, and a webservice tries to suggest values based on the information entered so far.</p> <p>My problem is that the webservice sometimes takes 2-3 seconds to generate the data, and the typeahead list is not filled in time.</p> <pre><code> &lt;form data-ng-submit="selectItem(message);"&gt; &lt;input type="text" data-ng-model="message" data-ng-change="handleChange(message);" data-typeahead-wait-ms="500" data-typeahead="o for o in searchOptions | filter:$viewValue | limitTo:8"&gt; &lt;input type="submit" style="height: 0px; width: 0px; border: none; padding: 0px;" data-hidefocus="true" /&gt; &lt;/form&gt; </code></pre> <p>I could change the <code>typeahead-wait-ms</code> to 3000, but that would make the other requests very slow (the requests are normaly 100-150ms). What I would like to do is to force angularjs to redraw the typeahead options once it has the data in <code>$scope.searchOptions</code>. Is there any way to do that?</p> <p>I tried to use <code>ng-show</code> (hiding and showing), tried to change the <code>$scope.message</code> (add and remove space), and nothing forced the regeneration of display so far.</p> <p><strong>Update:</strong> </p> <p>The javascript part is as follows (there is a delay counter which calls the triggerSearch only when the search Field hasn't updated since 500msec, i removed it for simplicity):</p> <pre><code>var triggerSearch = function() { searchService.setString($scope.message); // this gives the search string to service $injector.get('$rootScope').$broadcast('startSearch'); }; </code></pre> <p>then this message is listened in another controller:</p> <pre><code>$scope.$on('startSearch', function() { // here there is a $http.get which gets all the data after reading search string searchService.setOptions(selectionlist); // this gives back the result $injector.get('$rootScope').$broadcast('updatedOptions'); // calls the original controller } </code></pre> <p>And then in the original controller</p> <pre><code>$scope.$on('updatedOptions', function() { $scope.searchOptions = searchService.getOptions(); // reading the list from service $scope.$apply(); // tried this - didnt work. }); </code></pre> <p>The thing is, that when I add the result of the service in a normal data on the page, it gets correctly displayed when the data becomes available, i.e. 2 seconds later. However the typeahead list is not updated.</p> <p><strong>Update 2</strong></p> <p>Just to be more specific about last sentence:</p> <pre><code>&lt;form data-ng-submit="selectItem(message);"&gt; &lt;input type="text" data-ng-model="message" data-ng-change="handleChange(message);" data-typeahead-wait-ms="500" data-typeahead="o for o in searchOptions | filter:$viewValue | limitTo:8"&gt; &lt;input type="submit" style="height: 0px; width: 0px; border: none; padding: 0px;" data-hidefocus="true" /&gt; &lt;/form&gt; {{searchOptions}} </code></pre> <p>If I add this last line, this is correctly updated when the service has finished filling this data. It's only the typeahead which is not refreshed. So in the case above, I start typing the search substring, when the search has finished the {{searchOptions}} is correctly updated with search results. </p> <p>The question is: is there any way to force the regeneration of the input field, so that the typeahead tag with the new data is taken into consideration?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload