Get Value From Select Option in Angular 4

前端 未结 5 1126
难免孤独
难免孤独 2020-11-28 10:09

How do I get the value from the select option in Angular 4?

I want to assign it to a new variable in the component.ts file. I\'ve tried this but outputs nothing.

相关标签:
5条回答
  • 2020-11-28 10:52

    As a general (see Stackblitz here: https://stackblitz.com/edit/angular-gh2rjx):

    HTML

    <select [(ngModel)]="selectedOption">
       <option *ngFor="let o of options">
          {{o.name}}
       </option>
    </select>
    <button (click)="print()">Click me</button>
    
    <p>Selected option: {{ selectedOption }}</p>
    <p>Button output: {{ printedOption }}</p>
    

    Typescript

    export class AppComponent {
      selectedOption: string;
      printedOption: string;
    
      options = [
        { name: "option1", value: 1 },
        { name: "option2", value: 2 }
      ]
      print() {
        this.printedOption = this.selectedOption;
      }
    }
    

    In your specific case you can use ngModel like this:

    <form class="form-inline" (ngSubmit)="HelloCorp()">
         <div class="select">
             <select [(ngModel)]="corporationObj" class="form-control col-lg-8" #corporation required>
                 <option *ngFor="let corporation of corporations"></option>    
             </select>
             <button type="submit" class="btn btn-primary manage">Submit</button>
         </div>
    </form>
    
    HelloCorp() {
      console.log("My input: ", corporationObj);
    }
    
    0 讨论(0)
  • 2020-11-28 10:53

    You just need to put [(ngModel)] on your select element:

    <select class="form-control col-lg-8" #corporation required [(ngModel)]="selectedValue">
    
    0 讨论(0)
  • 2020-11-28 10:55

    export class MyComponent implements OnInit {
    
      items: any[] = [
        { id: 1, name: 'one' },
        { id: 2, name: 'two' },
        { id: 3, name: 'three' },
        { id: 4, name: 'four' },
        { id: 5, name: 'five' },
        { id: 6, name: 'six' }
      ];
      selected: number = 1;
    
      constructor() {
      }
      
      ngOnInit() {
      }
      
      selectOption(id: number) {
        //getted from event
        console.log(id);
        //getted from binding
        console.log(this.selected)
      }
    
    }
    <div>
      <select (change)="selectOption($event.target.value)"
      [(ngModel)]="selected">
      <option [value]="item.id" *ngFor="let item of items">{{item.name}}</option>
       </select>
    </div> 

    0 讨论(0)
  • 2020-11-28 10:56

    HTML code

        <form class="form-inline" (ngSubmit)="HelloCorp(modelName)">
            <div class="select">
                <select class="form-control col-lg-8" [(ngModel)]="modelName" required>
                    <option *ngFor="let corporation of corporations" [ngValue]="corporation">
                        {{corporation.corp_name}}
                    </option>    
                </select>
                <button type="submit" class="btn btn-primary manage">Submit</button>
            </div> 
        </form>
    

    Component code

    HelloCorp(corporation) {
        var corporationObj = corporation.value;
    }
    
    0 讨论(0)
  • 2020-11-28 11:09

    This is very simple actually.

    Please notice that I'm

    I. adding name="selectedCorp" to your select opening tag, and

    II. changing your [value]="corporationObj" to [value]="corporation", which is consistent with the corporation in your *ngFor="let corporation of corporations" statement:

     <form class="form-inline" (ngSubmit)="HelloCorp(f)" #f="ngForm">
       <div class="select">
         <select class="form-control col-lg-8" #corporation name="selectedCorp" required>
           <option *ngFor="let corporation of corporations" [value]="corporation">{{corporation.corp_name}}</option>
         </select>
         <button type="submit" class="btn btn-primary manage">Submit</button>
       </div>
     </form>
    

    And then in your .ts file, you just do the following:

    HelloCorp(form: NgForm) {
       const corporationObj = form.value.selectedCorp;
    }
    

    and the const corporationObj now is the selected corporation object, which will include all the properties of the corporation class you have defined.

    NOTE:

    In the html code, by the statement [value]="corporation", the corporation (from *ngFor="let corporation of corporations") is bound to [value], and the name property will get the value.

    Since the name is "selectedCorp", you can get the actual value by getting "form.value.selectedCorp" in your .ts file.

    By the way, actually you don't need the "#corporation" in your "select" opening tag.

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