Set ng-selected in angular-ui select

spyderReloaded picture spyderReloaded · Apr 9, 2014 · Viewed 8.8k times · Source

My HTML code:

<div class="form-group col-md-4">
      <label for="lenderBusinessState">Select State</label>
      <div class="input-group select2-bootstrap-append">

        <ui-select name="lenderBusinessState" ng-model="lenderInfo.modelState.selected" theme="select2" class="form-control">
          <match placeholder="Select Lender State">{{$select.selected.stateName}}</match>
          <choices repeat="state in states | filter: $select.search">
            <span ng-bind-html="state.stateName | highlight: $select.search" ng-selected="selectState(state.stateId)"></span>
          </choices>
        </ui-select>

        <span class="input-group-btn">
          <button type="button" ng-click="lenderInfo.modelState.selected = undefined" class="btn btn-danger">
            <span class="glyphicon glyphicon-trash"></span>
          </button>
        </span>
      </div>
    </div>

My JS code:

$scope.selectState = function(stateIdPassed)
{
    console.log(stateIdPassed);
    if(stateIdPassed == $scope.stateId){
        return 'selected';
    }
}

I want to prefill my ui-select input. I am using ui-select. If i don't use ui-select input and just use tags, then its working. But, not with tags.

What are the possible solutions? Please help me out as i have used in my application.

Answer

Richard Torcato picture Richard Torcato · May 27, 2014

just set lenderInfo.modelState.selected with the value you want preselected.

here is some code for you to try just throw these lines where you setup the controller

$scope.lenderInfo.modelState = {};
$scope.lenderInfo.modelState.selected =  {"stateId":1,"stateName":"Florida"};