1

I have created an Angular project with angular material and I want to display a list of units inside a dropdown.

the data comes like expected like this :

enter image description here

this is my .ts code :

ListUnits:Array<Unit>=[]

units(){
return this.trq.getListUnit().subscribe(
  (data)=>{
    this.ListUnits.push(data.respModel);
    console.log(this.ListUnits);
  }
)

}

and here is my .HTML code :

<mat-label>Unite</mat-label>
            <mat-select formControlName= "unite_id" required>
                <mat-option *ngFor="let unit of ListUnits"[value]="unit.id">
                  {{unit.unitName}}
                </mat-option>
            </mat-select>

but unfortunately, nothing appears Which are the steps to follow to achieve that?

1
  • Can you attach the API response as JSON snippet to the question? Look like it is a nested array. Thanks. Commented Mar 7, 2022 at 11:20

2 Answers 2

2

You are pushing array inside array. Instead do this if you want to retain current data in ListUnits

this.ListUnits = [...this.ListUnits, ...data.respModel];

Otherwise simply assign array from response to ListUnits

this.ListUnits = data.respModel;
Sign up to request clarification or add additional context in comments.

1 Comment

TimeArray = [{id:1,name:'test1'},{id:2,name:'test2'}];
-2

sample json array declaration

TimeArray = [{id:1,name:'test1'},{id:2,name:'test2'}];

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.