0

there is a template-driven form and within this form, there is a dropdown. I want to show data from the backend to the dropdown.

Here data is coming from the backend in appliesWhenData.

<form #appliesWhen="ngForm">
        <section class="form-inline" *ngFor="let awhen of appliesWhenData; let i = index;">
          <div class="col-auto my-1">
            <select class="custom-select mr-sm-2 form-control">
              <option [ngValue]="" disabled >Select field</option>
              <option [ngValue]="f" *ngFor="let f of fieldList">{{f.displayName}}
              </option>
            </select>
          </div>
</section>
</form>

and if I log appliesWhenData it will print:

[
  {
    "id": 1,
    "detailSrl": 5,
    "value": "123",
    "local_id": 1,
    "fieldListDetail": {
      "id": 5,
      "fieldName": "officeLocation",
      "displayName": "OfficeLocation"
    },
    "conditionListDetail": {
      "id": 1,
      "condition": "Equals"
    }
  },
  {
    "id": 2,
    "detailSrl": 5,
    "value": "321",
    "local_id": 2,
    "fieldListDetail": {
      "id": 3,
      "fieldName": "productType",
      "displayName": "ProductType"
    },
    "conditionListDetail": {
      "id": 4,
      "condition": "LessThanEquals"
    }
  }
]

What I have tried so far:

<form #appliesWhen="ngForm">
        <section class="form-inline" *ngFor="let awhen of appliesWhenData; let i = index;">
          <div class="col-auto my-1">
            <select class="custom-select mr-sm-2 form-control" [(ngModel)]="awhen.fieldListDetail" name="fieldName{{ awhen.id }}">
              <option [ngValue]="" disabled >Select field</option>
              <option [ngValue]="f" *ngFor="let f of fieldList">{{f.displayName}}
              </option>
            </select>
          </div>
</section>
</form>

Only I need to show selected data in drop. The selected data coming from backend is in appliesWhenData.fieldListDetail and the list which will be there is fieldList. Please help!

5
  • You are using *ngFor="let f of fieldList". What kind of data is there in fieldList. Commented May 10, 2021 at 3:49
  • Can you tell what are the options and value need to use from that response? Commented May 10, 2021 at 3:50
  • @RK001 actually, is a dropdown so the user can update the data. first, it will show selected data which is in appliesWhenData and the list is in field list. Commented May 10, 2021 at 3:59
  • @sankasanjeewa sorry didn't get your question. Commented May 10, 2021 at 4:00
  • @SandeepKumar should *ngFor="let f of fieldList" it should be *ngFor="let f of fieldListDetail" as per the log you have printed and more over fieldListDetail should be a type of array i guess Commented May 10, 2021 at 4:21

1 Answer 1

0

Please try binding data like this:

          <option [ngValue]="f" *ngFor="let f of fieldList">{{f.fieldListDetail.displayName}}
          </option>
Sign up to request clarification or add additional context in comments.

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.