Binding select element to object in Angular

后端 未结 14 1508
走了就别回头了
走了就别回头了 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:36

    This code is very simple:

    <select class="form-control" id="marasemaat" style="margin-top: 10%;font-size: 13px;" [(ngModel)]="fullNamePresentor" [formControl]="stateControl"  
                (change)="onSelect($event.target.value)">
                <option *ngFor="let char of programInfo1;let i = index;"   onclick="currentSlide(9,false)" value={{char.id}}>{{char.title + " "}}  ----> {{char.name + " "+ char.family }} ---- > {{(char.time.split('T', 2)[1]).split(':',2)}}</option>
      
              </select>
    
    0 讨论(0)
  • 2020-11-22 03:39

    In app.component.html:

     <select type="number" [(ngModel)]="selectedLevel">
              <option *ngFor="let level of levels" [ngValue]="level">{{level.name}}</option>
            </select>
    

    And app.component.ts:

    import { Component } from '@angular/core';
    
    @Component({
      selector: 'my-app',
      templateUrl: './app.component.html',
      styleUrls: [ './app.component.css' ]
    })
    export class AppComponent  {
      levelNum:number;
      levels:Array<Object> = [
          {num: 0, name: "AA"},
          {num: 1, name: "BB"}
      ];
    
      toNumber(){
        this.levelNum = +this.levelNum;
        console.log(this.levelNum);
      }
    
      selectedLevel = this.levels[0];
    
      selectedLevelCustomCompare = {num: 1, name: "BB"}
    
      compareFn(a, b) {
        console.log(a, b, a && b && a.num == b.num);
        return a && b && a.num == b.num;
      }
    }
    
    0 讨论(0)
  • 2020-11-22 03:41
    <h1>My Application</h1>
    <select [(ngModel)]="selectedValue">
      <option *ngFor="let c of countries" [ngValue]="c">{{c.name}}</option>
    </select>
    

    StackBlitz example

    NOTE: you can use [ngValue]="c" instead of [ngValue]="c.id" where c is the complete country object.

    [value]="..." only supports string values
    [ngValue]="..." supports any type

    update

    If the value is an object, the preselected instance needs to be identical with one of the values.

    See also the recently added custom comparison https://github.com/angular/angular/issues/13268 available since 4.0.0-beta.7

    <select [compareWith]="compareFn" ...
    

    Take care of if you want to access this within compareFn.

    compareFn = this._compareFn.bind(this);
    
    // or 
    // compareFn = (a, b) => this._compareFn(a, b);
    
    _compareFn(a, b) {
       // Handle compare logic (eg check if unique ids are the same)
       return a.id === b.id;
    }
    
    0 讨论(0)
  • 2020-11-22 03:41

    You Can Select the Id using a Function

    <option *ngFor="#c of countries" (change)="onchange(c.id)">{{c.name}}</option>
    
    0 讨论(0)
  • 2020-11-22 03:42

    Also, if nothing else from given solutions doesn't work, check if you imported "FormsModule" inside of "AppModule", that was a key for me.

    0 讨论(0)
  • 2020-11-22 03:43

    Create another getter for selected item

    <form [formGroup]="countryForm">
      <select formControlName="country">
        <option *ngFor="let c of countries" [value]="c.id">{{c.name}}</option>
      </select>
    
      <p>Selected Country: {{selectedCountry?.name}}</p>
    </form>
    

    In ts :

    get selectedCountry(){
      let countryId = this.countryForm.controls.country.value;
      let selected = this.countries.find(c=> c.id == countryId);
      return selected;
    }
    
    0 讨论(0)
提交回复
热议问题