简体   繁体   English

如何在单击 angular 7 中的按钮时获取下拉列表的选定值和选定文本

[英]How to get selected value and selected text of a dropdown on click a button in angular 7

I have a dropdown select,Here on click the button I need to console selected text(green..) and selected value(1..),I tried with ngmodel but I can able to capture only value and also empty option is showing there in select field.Here is the code below,我有一个下拉菜单 select,在这里单击按钮,我需要控制台选择的文本(绿色..)和选择的值(1..),我尝试使用 ngmodel 但我只能捕获值并且那里显示空选项在 select 字段中。这是下面的代码,

home.component.html home.component.html

<select>
<option *ngFor="let status of statusdata" value="{{status.id}}">{{status.name}}</option>
</select>
<button type="submit" (click)="register()" class="btn btn-primary mr-1">Register</button>

home.component.ts home.component.ts

import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit { 
    dotsh:any;
    hoverIndex:number = -1;
    groups:any;
    test:any;
 constructor(private formBuilder: FormBuilder) {


     }
     onHover(i:number){
 this.hoverIndex = i;
}
     columns = ["name", "Items","status"];



  ngOnInit() {
      this.test = false;
      this.statusdata = [{"id":1,"name":"green"},{"id":2,"name":"red"},{"id":3,"name":"yellow"}];
} 

register(){
    console.log('selected Value');
    console.log('selected name');
}


}

If what you want is to store the whole object as a value when selected, you can try this:如果您想要在选择时将整个 object 存储为一个值,您可以试试这个:

<select [(ngModel)]="value">
  <option *ngFor="let status of statusdata" [ngValue]="status"> 
   {{status.name}}</option>
</select>

And then in your component just declare the variable value, and either leave it undefined or assign it to the value you wish (it needs to be the whole object).然后在你的组件中声明变量值,或者让它未定义,或者将它分配给你想要的值(它需要是整个对象)。 For instance:例如:

export class HomeComponent implements OnInit { 
  statusdata = [{"id":1,"name":"green"},{"id":2,"name":"red"},{"id":3,"name":"yellow"}];
  value = statusdata[0];
  ....

That should default to the first option selected.这应该默认为选择的第一个选项。 Then if you want to print the value just:然后,如果您只想打印该值:

console.log(this.value.id);
console.log(this.value.name);

To achieve expected result, use below option of using Formbuilder, FormGroup and Form Control name in component.html and commponent.ts为了达到预期的效果,请使用以下选项,在 component.html 和 commponent.ts 中使用 Formbuilder、FormGroup 和 Form Control 名称

component.html组件.html

  1. Use form name for your form as [formGroup]="profileForm"使用表单名称作为[formGroup]="profileForm"
  2. Set formControlName for select ie formControlName="name"为 select 设置 formControlName 即formControlName="name"

`component.ts `组件.ts
3. Declare formgroup and using formbuilder initialize name 3.声明formgroup并使用formbuilder初始化名称

profileForm: FormGroup
 constructor(private formBuilder: FormBuilder) {
this.profileForm = this.formBuilder.group({
    name: new FormControl(1)
});  

4. On button click get value using this.profileForm.get('name').value 4. 在按钮上单击使用this.profileForm.get('name').value获取值

register(){
    console.log('selected Value', this.profileForm.get('name').value);
    console.log('selected name', this.statusdata.filter(v => v.id == this.profileForm.get('name').value)[0].name);
}

Sample working code for reference - https://stackblitz.com/edit/angular-unnal4?file=src/app/app.component.ts示例工作代码供参考 - https://stackblitz.com/edit/angular-unnal4?file=src/app/app.component.ts

Please refer this link for more details on reactive forms - https://angular.io/guide/reactive-forms有关反应式 forms - https://angular.io/guide/reactive-forms的更多详细信息,请参阅此链接

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从页面加载的下拉列表中获取选定的值和文本,然后单击具有 angular 8 响应形式的按钮 - How to get the selected value and text from a dropdown on page load and on click a button with reactive form in angular 8 如何在jquery中单击按钮时获取所选下拉值的值 - How to get value of selected dropdown value on button click in jquery 如何在angular6中单击按钮时获得多个下拉列表的选定值 - How to get the selected value of multiple dropdown onclick a button in angular6 单击按钮即可从最接近的所选下拉选项中获取文本 - Get text from closest selected dropdown option with class on button click 如何在angularjs中单击按钮获得选定的值? - how to get selected value on button click in angularjs? 单击 angular 8 中的按钮时,如何从多个下拉列表中删除所选选项 - How to remove selected option from multiple dropdown on click a button in angular 8 我正在尝试获取文本,但是我获得了?(值)并且无法单击。 这是硒中的一个下拉按钮(默认情况下,值为) - I am trying to get text but i got ?(value) and unable to click. And this is a dropdown button (by default on value is selected ) in selenium 如何在Angular JS的输入文本字段中显示下拉列表的选定值 - how to display selected value of dropdown in input text fields in angular js 显示下拉至按钮的选定文本值 - Display selected text value of dropdown to button 如何在加载时获得角度6下拉列表的选定值(不更改) - How to get selected value of dropdown in angular 6 on load(not onchange)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM