24

I'm using angular bootstrap typeahead. I would like to add a functionality for infinite scroll in that, for that i have added a small directive for scroll functionality and some changes in typeahead template, scroll directive works as expected but typeahead not updating a view after result is update.

My input is like :

<input type="text" ng-model="variable" placeholder="Search..." typeahead="obj as obj.text for obj in getObject($viewValue)" typeahead-wait-ms="500" />

typeahead template :

<ul class="dropdown-menu typeahead-custom" ng-scroll="getObject('{{query}}')" ng-scroll-direction="down" ng-show="isOpen()" ng-style="{top: position.top+'px', left: position.left+'px'}" style="display: block;" role="listbox" aria-hidden="{{!isOpen()}}">
    <li ng-repeat="match in matches track by $index" ng-class="{active: isActive($index) }" ng-mouseenter="selectActive($index)" ng-click="selectMatch($index)" role="option" id="{{match.id}}" should-focus="isActive($index)">
        <div typeahead-match index="$index" match="match" query="query" template-url="templateUrl"></div>
    </li>
</ul>

and getObject Function :

$rootScope.lastResult = [];
$rootScope.getObject = function(str) {
    return  $http({
        url: "someUrl",
        method: "post",
        headers: {'Content-Type': 'application/json'},
        data: {str: str, page_limit: 10}
    }).then(function(response) {
        $.merge($rootScope.lastResult, response.data.data);
        return $rootScope.lastResult;
    });
};

when I type something in typeahead input it works fine, but when i scroll into the typeahead template it calls a function and updating lastResult variable but didn't updating a DOM element of typeahead.

Any help? Please....

Arjun
  • 1,431
  • 1
  • 12
  • 32
  • 1
    have you tried `$scope.apply()` after result update? – Anita Sep 01 '15 at 09:57
  • 1
    [visit this link https://github.com/angular-ui/bootstrap/blob/master/src/typeahead/typeahead.js You might want to edit that file – samir Sep 02 '15 at 04:06

4 Answers4

1

Please try this one !!

$rootScope.lastResult = [];
$rootScope.getObject = function(str) {
    return  $http({
        url: "someUrl",
        method: "post",
        headers: {'Content-Type': 'application/json'},
        data: {str: str, page_limit: 10}
    }).then(function(response) {
        $rootScope.$apply(function(){
         $.merge($rootScope.lastResult, response.data.data);
        });
        return $rootScope.lastResult;
    });
};
lxg
  • 12,375
  • 12
  • 51
  • 73
Amit Kumar
  • 46
  • 5
0

I had written something similar but instead of using scroll, there was a button which was clicked.

UI Bootstrap does not expose any API of their directives and as such I was required to extend their template, and add an additional controller via a directive which would manipulate the data as sent to the type ahead directive.

In short, it was dirty, and I would recommend trying to find another library for type ahead functionality other than UI Bootstrap.

Brandon Brooks
  • 271
  • 1
  • 6
0

check whether you are binding variable of rootscope or scope. after any change in scope or rootscope just apply it just like $scope.apply(). so it'll call $apply/$digest internally and reflect it in UI.

Shrinath
  • 364
  • 1
  • 13
0

To get this to work, I had to edit the UI bootstrap source, adding an event listener to the UibTypeaheadController. Above the getMatchesAsync function:

originalScope.$on('loadMore', function() {
  hasFocus = true;
  getMatchesAsync(modelCtrl.$viewValue);
});

When the user scrolls to the bottom, I emit the load more event scope.$emit('loadMore'). This allows the list of matches to be updated on scroll.

JamesIngold
  • 374
  • 2
  • 8