Search code examples
angularjsasynchronousng-options

How to set default value in asynchronous ng-options


The ng-options get asynchronous . How can I set the default value?

<select class="form-control" 
        ng-options="item.id as item.value for item in mInsu.INS_MAX_AMOUNT track by item.id"
        ng-model="custInfo.mInsu.selAmount" required>
</select>

I tried to do like this:

<select class="form-control" ng-model="custInfo.mInsu.selAmount" required>
    <option ng-repeat="item in mInsu.INS_MAX_AMOUNT" value="{{item.id}}">{{item.value}}</option>
</select>

Luckly, it can meet my requirement.

So, why can't set default value in asynchronous ng-options?


Solution

  • Add the below lines in your controller:

    $scome.custInfo.mInsu.selAmount = mInsu.INS_MAX_AMOUNT[0].id;
    

    which assigns the first value as default.