javascriptangularjsangular-uiangular-ui-select

Manually entering text in angular-ui


My question comes from the selected answer here

<ui-select-choices repeat="hero in getSuperheroes($select.search) | filter: $select.search">
<div ng-bind="hero"></div>

works perfectly fine with:

$scope.getSuperheroes = function(search) {
  var newSupes = $scope.superheroes.slice();
  if (search && newSupes.indexOf(search) === -1) {
    newSupes.unshift(search);
  }
  return newSupes;
}

However, when I put a breakpoint on $scope.superheroes I see its called as many times as my data. Data is very large. Its fetched from a $http.get() request and I'm handling not loading the data in the textbox all at once.

However, I don't want to call this until I actually start typing anything in the textbox. I tried to call getSuperheroes using

on-select="getSuperheroes($select.search)" 

and similarly

refresh="getSuperheroes($select.search)"

but they do not help allowing manual entry of data.

What do I use to call the function and yet get the task done just like how it works in the referenced answer?


Solution

  • I recommend you to do this:

    $scope.getSuperheroes = function(search) {
        var aux = $scope.superheroes.slice();
        if (search && aux.indexOf(search) === -1) {
            aux.unshift(search);
        }
        $scope.myHeroes = aux;
    }
    

    PS:

    -- If you want to get the ui-select with some values initially, You must call the getSuperheroes func on your controller start.

    -- You may need to declare $scope.myHeroes = []; at the beginning of your controller (it depends on your controller implementation).

    -- You might want to read more info about $digest on the AngularJS official doc, on SO there are some related posts: