Binding select element to object in Angular

后端 未结 14 1507
走了就别回头了
走了就别回头了 2020-11-22 02:41

I\'d like to bind a select element to a list of objects -- which is easy enough:

@Component({
   selector: \'myApp\',
   template: `

My Application&

相关标签:
14条回答
  • 2020-11-22 03:21

    It worked for me:

    Template HTML:

    I added (ngModelChange)="selectChange($event)" to my select.

    <div>
      <label for="myListOptions">My List Options</label>
      <select (ngModelChange)="selectChange($event)" [(ngModel)]=model.myListOptions.id >
        <option *ngFor="let oneOption of listOptions" [ngValue]="oneOption.id">{{oneOption.name}}</option>
      </select>
    </div>
    

    On component.ts:

      listOptions = [
        { id: 0, name: "Perfect" },
        { id: 1, name: "Low" },
        { id: 2, name: "Minor" },
        { id: 3, name: "High" },
      ];
    

    An you need add to component.ts this function:

      selectChange( $event) {
        //In my case $event come with a id value
        this.model.myListOptions = this.listOptions[$event];
      }
    

    Note: I try with [select]="oneOption.id==model.myListOptions.id" and not work.

    ============= Another ways can be: =========

    Template HTML:

    I added [compareWith]="compareByOptionId to my select.

    <div>
      <label for="myListOptions">My List Options</label>
      <select [(ngModel)]=model.myListOptions [compareWith]="compareByOptionId">
        <option *ngFor="let oneOption of listOptions" [ngValue]="oneOption">{{oneOption.name}}</option>
      </select>
    </div>
    

    On component.ts:

      listOptions = [
        { id: 0, name: "Perfect" },
        { id: 1, name: "Low" },
        { id: 2, name: "Minor" },
        { id: 3, name: "High" },
      ];
    

    An you need add to component.ts this function:

     /* Return true or false if it is the selected */
     compareByOptionId(idFist, idSecond) {
        return idFist && idSecond && idFist.id == idSecond.id;
     }
    
    0 讨论(0)
  • 2020-11-22 03:23

    For me its working like this, you can console event.target.value.

    <select (change) = "ChangeValue($event)" (ngModel)="opt">   
        <option *ngFor=" let opt of titleArr" [value]="opt"></option>
    </select>
    
    0 讨论(0)
  • 2020-11-22 03:25

    This could help:

        <select [(ngModel)]="selectedValue">
              <option *ngFor="#c of countries" [value]="c.id">{{c.name}}</option>
        </select>
    
    0 讨论(0)
  • 2020-11-22 03:27

    use this way also..

    <h1>My Application</h1>
    <select [(ngModel)]="selectedValue">
         <option *ngFor="let c of countries" value="{{c.id}}">{{c.name}}</option>
     </select>
    
    0 讨论(0)
  • 2020-11-22 03:29

    You can get selected value also with help of click() by passing the selected value through the function

    <md-select placeholder="Select Categorie"  
        name="Select Categorie" >
      <md-option *ngFor="let list of categ" [value]="list.value" (click)="sub_cat(list.category_id)" >
        {{ list.category }}
      </md-option>
    </md-select>
    
    0 讨论(0)
  • 2020-11-22 03:35
    
    <select name="typeFather"
        [(ngModel)]="type.typeFather">
            <option *ngFor="let type of types" [ngValue]="type">{{type.title}}</option>
    </select>
    

    that approach always gonna work, however If you have a dynamic list, make sure you load it before the model

    0 讨论(0)
提交回复
热议问题