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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With