简体   繁体   中英

get the value of the selected option from the drop down

I am a newbie to angular. I have a bootstrap drop-down and i need to get the value of the selected option from that dropdown. thanks in advance.

 FacultyorStudent_Data: Array<string> = ['Faculty/Coach','Student'] selected_FacultyorStudent: string = this.FacultyorStudent_Data[0]; SelectFacultyorStudent(FnS){ console.log(FnS) }
 <div class="text-center" id="perf-type"> <div class="dropdown-toggle" data-toggle="dropdown">{{selected_FacultyorStudent}}&nbsp; Insights<i class="fa fa-caret-down"></i> </div> <ul class="dropdown-menu"> <li *ngFor="let item of FacultyorStudent_Data; let i = index;" [ngClass]="{'bg-selected-subject': selected_FacultyorStudent==item}" (click)="SelectFacultyorStudent(FnS)"> {{item}}</li> </ul> </div>

Pass item as an argument of SelectFacultyorStudent like (click)="SelectFacultyorStudent(item)

pass item instead of fns in html file

<div class="text-center" id="perf-type">
        <div class="dropdown-toggle" data-toggle="dropdown">{{selected_FacultyorStudent}}&nbsp; Insights<i
            class="fa fa-caret-down"></i>
        </div>
        <ul class="dropdown-menu">
          <li *ngFor="let item of FacultyorStudent_Data; let i = index;"
            [ngClass]="{'bg-selected-subject': selected_FacultyorStudent==item}"
            (click)="SelectFacultyorStudent(item)">
            {{item}}</li>
        </ul>
      </div>


FacultyorStudent_Data: Array<string> = ['Faculty/Coach','Student']
  selected_FacultyorStudent: string = this.FacultyorStudent_Data[0];

  SelectFacultyorStudent(FnS){

    console.log(FnS)
  }

Try this one. I have passed item instead of an FnS from your HTML. And Received the value in TS file and assign the value to the selected_FacultyorStudent this variable.

 <div class="text-center" id="perf-type">
<div class="dropdown-toggle" data-toggle="dropdown">{{selected_FacultyorStudent}}&nbsp; Insights<i
    class="fa fa-caret-down"></i>
</div>
<ul class="dropdown-menu">
  <li *ngFor="let item of FacultyorStudent_Data; let i = index;"
    [ngClass]="{'bg-selected-subject': selected_FacultyorStudent==item}"
    (click)="SelectFacultyorStudent(item)">
    {{item}}</li>

</ul>

  FacultyorStudent_Data: Array<string> = ['Faculty/Coach','Student']
  selected_FacultyorStudent: string = this.FacultyorStudent_Data[0];

  SelectFacultyorStudent(FnS){
   this.selected_FacultyorStudent = FnS;
    console.log(FnS)
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM