Angular 2: How to get the selected value from different options of a form?

前端 未结 4 821
醉酒成梦
醉酒成梦 2020-12-16 16:58

I would like to use a

提交评论

  • 2020-12-16 17:50

    In fact I can't reproduce your problem. I created a plunkr with a very simple form with an input and a select. When I submit the form, I have actual values in the bound object. See this plunkr: https://plnkr.co/edit/5C3agW7QZfcrdt88QzSh?p=preview.

    Feel free to tell me if I didn't correctly understand your problem.

    Thierry

    0 讨论(0)
  • 2020-12-16 17:53

    If you have static, hard-coded values for the select tag like below:

    <select #quantity>
      <option value="one">1</option>
      <option value="two">2</option>
      <option value="three">3</option>
      <option value="four">4</option>
      <option value="five">5</option>
    </select>
    

    You can do the following:

    @ViewChild('quantity') quantity: ElementRef;
    
    console.log(this.quantity.nativeElement.value);  // will print value of the currently selected option
    
    0 讨论(0)
  • 2020-12-16 17:59

    There is a way to get the value from different options. check this plunker

    component.html

     <select class="form-control" #t (change)="callType(t.value)">
          <option *ngFor="#type of types" [value]="type">{{type}}</option>
        </select>
    

    component.ts

    this.types = [ 'type1', 'type2', 'type3' ];
       this.order = {
          type: 'type1'          
      };  
    
      callType(value){
        console.log(value);
        this.order.type=value;
      }
    
    0 讨论(0)
  • 提交回复
    热议问题