0

I am trying to concat two fields but the problem is in some cases the second field doesn't exist. In the below code group.GroupDescription field is not present in some entires. Is there any way to concat both fields only if second field group.GroupDescriptionexists.

Note - I also tried safe navigation operator but it displays null if value is not present.

Thanks in adavnce

         <select
            [(ngModel)]="selectedGroup"
            class="form-control"
            (ngModelChange)="onChange($event)"
          >
            <option [ngValue]="undefined" selected>Select</option>

            <option *ngFor="let group of groups" [ngValue]="group">{{
              group.GroupName.S + ' - ' + group.GroupDescription?.S
            }}</option>
          </select>

2 Answers 2

2

A fast and simple solution could be:

<select [(ngModel)]="selectedGroup" class="form-control" (ngModelChange)="onChange($event)">
    <option [ngValue]="undefined" selected>Select</option>

    <option *ngFor="let group of groups" [ngValue]="group">
        {{
            group.GroupName.S + (group.GroupDescription ? " - " + group.GroupDescription.S : '')
        }}
    </option>
</select>

but I suggest you to pre-build the string somewhere else in your code.

Sign up to request clarification or add additional context in comments.

Comments

1
<ng-container *ngIf="group.GroupDescription">
  {{group.GroupName.S + ' - ' + group.GroupDescription.S}}
</ng-container>
<ng-container *ngIf="!group.GroupDescription">
  {{group.GroupName.S}}
</ng-container>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.