4

How can I use ui-select to get data from a REST service in real time?

There is an example of ui-select with preloaded data, but for the same case, let's say there are 1000 users, and you want to select 5 of them and search by name for example, can you call a service in real time? Somehow like typeahead.

1 Answer 1

10

From the documentation:

Setup your directive like so:

<ui-select multiple ng-model="yourmodel" theme="select2" ng-disabled="disabled" style="width: 800px;">
    <ui-select-choices repeat="address in addresses track by $index"
         refresh="refreshAddresses($select.search)"
         refresh-delay="0">
    </ui-select-choices>
</ui-select>

The refreshAddress function is going to be called when searching. Here is what that would look like with an async call to the server:

function MyCtrl(){
   $scope.addresses = [];
   $scope.refreshAddresses = function(address) {
     var params = {address: address, sensor: false};
       return $http.get('http://maps.googleapis.com/maps/api/geocode/json', {params: params})
  .then(function(response) {
    $scope.addresses = response.data.results
  });
};
}

This example is calling a google api endpoint to get data. You would call your endpoint instead.

Here's a link to the documentation: https://github.com/angular-ui/ui-select/wiki/ui-select

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.