Get Value From Select Option in Angular 4

前端 未结 5 1132
难免孤独
难免孤独 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

    
    
    
    

    Selected option: {{ selectedOption }}

    Button output: {{ printedOption }}

    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:

    HelloCorp() { console.log("My input: ", corporationObj); }

提交回复
热议问题