Get selected value from a drop-down list in Angular

后端 未结 3 1584
遇见更好的自我
遇见更好的自我 2021-02-20 09:30

I am pretty new to Angular and have a requirement where I need to read the selected value from a drop-down list and send the selected value to a component while routing. Can som

3条回答
  •  滥情空心
    2021-02-20 10:18

    Here is the solutions for both your questions:

    • To get dropdown value.
    • To send params through route

    Solution to your first question binding dropdown:

    Component:

    import { Component } from '@angular/core';
    import { Router } from '@angular/router';
    
    
    @Component({
      selector: 'my-app',
      templateUrl: './app.component.html',
      styleUrls: [ './app.component.css' ]
    })
    export class AppComponent  {
    
      constructor(private route: Router){ }
    
      selectedLevel;
      data:Array = [
          {id: 0, name: "name1"},
          {id: 1, name: "name2"}
      ];
    
      selected(){
        console.log(this.selectedLevel)
      }
     }
    
    
    

    HTML:

    Drop Down

    {{selectedLevel | json}}

    Here is a working DEMO


    Solution to your second question To send Params to Route:

    From Component:

    this.route.navigate(['schedulemanagement/autoStartStopVm/' + data]);

    So, selected function will be,

      selected(){
        console.log(this.selectedLevel)
       this.route.navigate(['schedulemanagement/autoStartStopVm/' + data]);
      }
    

    From HTML:

    Person
    

    提交回复
    热议问题