0

I'm trying to implement a dropdown list with ngfor, but I have two issue.

  1. I don't get a default value selected on the first element. The array is having 4 element. With the first ngFor at the mat-select I want to select 4 items and the first item preferably being selected per default.

  2. There is being shown an unwanted empty arrow (line) at the end.

could someone please suggest a better solution. This is my implementations, which I have done after browsing so many post on SO on this topic.

  <mat-select *ngFor="let level of myArray; let i=index " placeholder="{{myArray[i+1].name}}">
           <mat-option *ngFor="let child of level.children | keyvalue" value="{{child.value.name}}" >
              {{child.value.name}}
           </mat-option>
            {{level.name}}
  </mat-select>

3 Answers3

0

Try to bind value with name: [value]="{{child.value.name}} instead of value="{{child.value.name}}"

That is in this line of code:

<mat-option *ngFor="let child of level.children | keyvalue" value="{{child.value.name}}" >
James Delaney
  • 1,765
  • 1
  • 17
  • 36
0

Provide the [(value)]='myDefaultValue on your <mat-select> and in your component.ts add the variable myDefaultValue = level.children[0].value.name

Bogdan B
  • 846
  • 9
  • 23
0

enter image description hereI hope you have the html and the array structure like this. If not please help me out I shall tweak the array structure as per your data.

// CSS
 .drop-down-selection{
     min-height: 30px;
     border: 1px solid #000;
     margin-top: 10px;
     margin-bottom: 10px;
  }
<mat-select *ngFor="let level of myArray; let i=index" placeholder="{{level.name}}" class="drop-down-selection" [(value)]="level.children[0]['name']">
   <mat-option *ngFor="let child of level.children | keyvalue" value="{{child.value.name}}">
      {{child.value.name}}
   </mat-option>
</mat-select>


    this.myArray = [
                    {
                           name: 'A',
                           children: [
                                   {
                                           name: 'One',
                                           desc: 'No description'
                                   },
                                   {
                                           name: 'Two',
                                           desc: 'No description'
                                   }
                           ] 
                    },
                    {
                           name: 'B',
                           children: [
                                   {
                                           name: 'three',
                                           desc: 'No description'
                                   },
                                   {
                                           name: 'four',
                                           desc: 'No description'
                                   }
                           ] 
                    },
                    {
                           name: 'C',
                           children: [
                                   {
                                           name: 'five',
                                           desc: 'No description'
                                   },
                                   {
                                           name: 'six',
                                           desc: 'No description'
                                   }
                           ] 
                    },
                    {
                           name: 'D',
                           children: [
                                   {
                                           name: 'seven',
                                           desc: 'No description'
                                   },
                                   {
                                           name: 'Two',
                                           desc: 'No description'
                                   }
                           ] 
                    },
                    {
                           name: 'E',
                           children: [
                                   {
                                           name: 'eight',
                                           desc: 'No description'
                                   },
                                   {
                                           name: 'nine',
                                           desc: 'No description'
                                   }
                           ] 
                    }
            ];

Let me know if this does not work. I shall tweak something otherwise.