Show default value in drop down text box when there is single value in list

If the drop down has only one value, then I need to show that value as default value in drop down.

Eg: If "Fruits" drop down has only 1 value, then I need to show that value as default value

<div *ngIf='items.length>0'>

<span *ngIf= "isAllFruitsSelected; else Fruitname">{{'ALL'}}</span>

<ng-template #Fruitname> {{this.form.value.selectedFruits.length}} + 'Fruit Selected'}} </ng-template> </div>

1 answer

  • answered 2022-01-19 16:45 Juan Vicente Berzosa Tejero

    try this:

    <ng-template #Fruitname>
    {{ this.form.value.selectedFruits.length = 1 ? this.form.value.selectedFruits[0]?.name : (this.form.value.selectedFruits.length + 'Fruit Selected') }}
    
    • If selectedFruits is an array of fruit names (strings), you don't need the ?.name. (just this.form.value.selectedFruits[0]).
    • If it is an object array, you should change the attribute 'name' in *?.name for the suitable attribute for your objects.

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum