Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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.

share|improve this question
up vote 6 down vote accepted

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.