Angular 2 select option (dropdown) - how to get the value on change so it can be used in a function?

前端 未结 6 912
小蘑菇
小蘑菇 2020-12-13 17:49

I am trying to build a drop down with a few values.

However, on selecting a value, I want to make an API call that takes an id.

In my component.ts, I have an

相关标签:
6条回答
  • 2020-12-13 18:19

    Another solution would be,you can get the object itself as value if you are not mentioning it's id as value: Note: [value] and [ngValue] both works here.

    <select (change)="your_method(values[$event.target.selectedIndex])">
      <option *ngFor="let v of values" [value]="v" >  
        {{v.name}}
      </option>
    </select>
    

    In ts:

    your_method(v:any){
      //access values here as needed. 
      // v.id or v.name
    }
    

    Note: If you are using reactive form and you want to catch selected value on form Submit, you should use [ngValue] directive instead of [value] in above scanerio

    Example:

      <select (change)="your_method(values[$event.target.selectedIndex])" formControlName="form_control_name">
          <option *ngFor="let v of values" [ngValue]="v" >  
            {{v.name}}
          </option>
        </select>
    

    In ts:

    form_submit_method(){
            let v : any = this.form_group_name.value.form_control_name;  
        }
    
    0 讨论(0)
  • 2020-12-13 18:20

    You need to use an Angular form directive on the select. You can do that with ngModel. For example

    @Component({
      selector: 'my-app',
      template: `
        <h2>Select demo</h2>
        <select [(ngModel)]="selectedCity" (ngModelChange)="onChange($event)" >
          <option *ngFor="let c of cities" [ngValue]="c"> {{c.name}} </option>
        </select>
      `
    })
    class App {
      cities = [{'name': 'SF'}, {'name': 'NYC'}, {'name': 'Buffalo'}];
      selectedCity = this.cities[1];
    
      onChange(city) {
        alert(city.name);
      }
    }
    

    The (ngModelChange) event listener emits events when the selected value changes. This is where you can hookup your callback.

    Note you will need to make sure you have imported the FormsModule into the application.

    Here is a Plunker

    0 讨论(0)
  • 2020-12-13 18:21

    Template/HTML File (component.ts)

    <select>
     <option *ngFor="let v of values" [value]="v" (ngModelChange)="onChange($event)">  
        {{v.name}}
      </option>
    </select>
    

    Typescript File (component.ts)

    values = [
      { id: 3432, name: "Recent" },
      { id: 3442, name: "Most Popular" },
      { id: 3352, name: "Rating" }
    ];
    
    onChange(cityEvent){
        console.log(cityEvent); // It will display the select city data
    }
    

    (ngModelChange) is the @Output of the ngModel directive. It fires when the model changes. You cannot use this event without the ngModel directive

    0 讨论(0)
  • 2020-12-13 18:21
    <select [(ngModel)]="selectedcarrera" (change)="mostrardatos()" class="form-control" name="carreras">
        <option *ngFor="let x of carreras" [ngValue]="x"> {{x.nombre}} </option>
    </select>
    

    In ts

    mostrardatos(){
    
    }
    
    0 讨论(0)
  • 2020-12-13 18:37
    values_of_objArray = [
      { id: 3432, name: "Recent" },
      { id: 3442, name: "Most Popular" },
      { id: 3352, name: "Rating" }
    ];
    
    private ValueId : number = 0 // this will be used for multi access like 
                                 // update, deleting the obj with id.
    private selectedObj : any;
    
    private selectedValueObj(id: any) {
      this.ValueId = (id.srcElement || id.target).value;
      for (let i = 0; i < this.values_of_objArray.length; i++) {
        if (this.values_of_objArray[i].id == this.ValueId) {
          this.selectedObj = this.values_of_objArray[i];
        }
      }
    }
    

    Now play with this.selectedObj which has the selected obj from the view.

    HTML:

    <select name="values_of_obj" class="form-control" [(ngModel)]="ValueId"  
            (change)="selectedValueObj($event)" required>
    
      <option *ngFor="let Value of values_of_objArray"
              [value]="Value.id">{{Value.name}}</option>    
    </select>
    
    0 讨论(0)
  • 2020-12-13 18:39

    My answer is little late but simple; but may help someone in future; I did experiment with angular versions such as 4.4.3, 5.1+, 6.x, 7.x, 8.x, 9.x and 10.x using $event (latest at the moment)

    Template:

    <select (change)="onChange($event)">
        <option *ngFor="let v of values" [value]="v.id">{{v.name}}</option>
    </select>
    

    TS

    export class MyComponent {
      public onChange(event): void {  // event will give you full breif of action
        const newVal = event.target.value;
        console.log(newVal);
      }
    }
    
    0 讨论(0)
提交回复
热议问题