Search code examples
angularjsangularjs-directiveangularjs-scope

Edit the selected option text in "ng-options" select-list


I need to add the text "(Default)" to the selected-option text. please help me...

The text should be "Visa 1881 (Default)".

But its only showing "Visa 1881". Here is my code:

<select ng-options="item.brand + ' ' + item.last_digits for item in approvedinvoicesCtrl.current_job.cards" ng-model="approvedinvoicesCtrl.GetCard"></select>

var vmc = this;

vms.current_job = response.data.payment_info;

for (var i = 0; i < vmc.current_job.cards.length; i++){
    if (vmc.current_job.cards[i].is_default){
        vmc.GetCard = vmc.current_job.cards[i] + ' (Default)';
    }
}      

What I get is this,

And what I need is this.

Thanks.


Solution

  • Try to initialize using ng-init default value:

    <select ng-init=" approvedinvoicesCtrl.GetCard = initialValue" 
            ng-model="approvedinvoicesCtrl.GetCard" 
            ng-options="item.brand + ' ' + item.last_digits for item in approvedinvoicesCtrl.current_job.cards">
    </select>
    

    Updated:

    jsfiddle link:

    https://jsfiddle.net/avnesh2/g2j6863a/6/
    

    working as you want.

    Hope this will work.