Set initially selected item in Select list in Angular2

后端 未结 7 1130
独厮守ぢ
独厮守ぢ 2020-12-07 23:59

I\'ve managed to get a Select list to bind with my model for the purpose of saving, but I cannot work out how to make Angular2 automatically select the correct option on the

相关标签:
7条回答
  • 2020-12-08 00:40

    Update to angular 4.X.X, there is a new way to mark an option selected:

    <select [compareWith]="byId" [(ngModel)]="selectedItem">
      <option *ngFor="let item of items" [ngValue]="item">{{item.name}}
      </option>
    </select>
    
    byId(item1: ItemModel, item2: ItemModel) {
      return item1.id === item2.id;
    }
    

    Some tutorial here

    0 讨论(0)
  • 2020-12-08 00:40

    If you use

    <select [ngModel]="object">
        <option *ngFor="let object of objects" [ngValue]="object">{{object.name}}</option>
    </select>
    

    You need to set the property object in you components class to the item from objects that you want to have pre-selected.

    class MyComponent {
      object;
      objects = [{name: 'a'}, {name: 'b'}, {name: 'c'}];
      constructor() {
        this.object = this.objects[1];
      }
    }
    
    0 讨论(0)
  • 2020-12-08 00:40

    The easiest way to solve this problem in Angular is to do:

    In Template:

    <select [ngModel]="selectedObjectIndex">
      <option [value]="i" *ngFor="let object of objects; let i = index;">{{object.name}}</option>
    </select>
    

    In your class:

    this.selectedObjectIndex = 1/0/your number wich item should be selected
    
    0 讨论(0)
  • 2020-12-08 00:41

    Will it work, if you just remove the edited item from the array, and push it at the beginning of the array, so it would be automatically selected?

    0 讨论(0)
  • 2020-12-08 00:43

    I hope it will help someone ! (works on Angular 6)

    I had to add lots of select/options dynamically and following worked for me:

    <div *ngFor="let option of model.q_options; let ind=index;">
    
            <select 
              [(ngModel)]="model.q_options[ind].type" 
              [ngModelOptions]="{standalone: true}"
            > 
              <option 
                *ngFor="let object of objects" 
                [ngValue]="object.type" 
                [selected]="object.type === model.q_options[ind].type"
              >{{object.name}}
              </option>
            </select> 
    
            <div [ngSwitch]="model.q_options[ind].type">
              ( here <div *ngSwitchCase="'text' or 'imagelocal' or etc."> is used to add specific input forms )
            </div>
    </div>
    

    and in *.ts

    // initial state of the model
    // q_options in html = NewOption and its second argument is option type
    model = new NewQuestion(1, null, 2, 
      [
        new NewOption(0, 'text', '', 1), 
        new NewOption(1, 'imagelocal', '', 1)
      ]);
    
    // dropdown options
    objects = [
        {type: 'text', name: 'text'},
        {type: 'imagelocal', name: 'image - local file'},
        {type: 'imageurl', name: 'image URL'}
       ( and etc.)
    ];
    

    When user adds one more 'input option' (pls do not confuse 'input option' with select/options - select/options are static here) specific select/option, selected by the user earlier, is preserved on each/all dynamically added 'input option's select/options.

    0 讨论(0)
  • 2020-12-08 00:46

    You can achieve the same using

    <select [ngModel]="object">
      <option *ngFor="let object of objects;let i= index;" [value]="object.value" selected="i==0">{{object.name}}</option>
    </select>
    
    0 讨论(0)
提交回复
热议问题