我正在尝试在Angular 4中获取Select控件的选定文本选项。
HTML:
<div class="col-sm-6 form-group">
<label>Industry</label>
<select class="form-control select" formControlName="Industry">
<option value="">Please select Value</option>
<option *ngFor="let industry of industries"
[ngValue]="industry.ID">{{industry.Name}}
</option>
</select>
</div>
upload.component.ts
this.form.controls['Industry'].valueChanges.subscribe((name) => {
this.form.controls['IndustryName'].setValue(name);
});
我正在使用Reactive的formControlName属性。
请建议回复选定文本选择控件
答案 0 :(得分:5)
实现这一目标的最简单方法是:从HTML获取id,使用id值引发事件,然后在集合中搜索该项目。
<强> HTML 强>
<select class="form-control" [(ngModel)]="selectedStatusId" (change)="setSelectedStatus(selectedStatusId)">
<option *ngFor="let status of statusList" [value]="status.id">{{status.name}}
</option>
</select>
<强>打字稿强>
public setSelectedStatus(value: string): void {
if (this.statusList && value) {
let status: ListItemSimplified = this.statusList.find(s => s.id == value);
if (status)
this.selectedStatus = status.name;
}
else
this.selectedStatus = '';
}
模型类
export class ListItemSimplified {
id: string;
name: string;
}
答案 1 :(得分:3)
getSelectedOptionText(event: Event) {
let selectedOptions = event.target['options'];
let selectedIndex = selectedOptions.selectedIndex;
let selectElementText = selectedOptions[selectedIndex].text;
console.log(selectElementText)
}
HTML
<select class="form-control select" formControlName="Industry" (change)="getSelectedOptionText($event)">
<option value="">Please select Value</option>
<option *ngFor="let industry of industries" value="{{industry.ID}}">{{industry.Name}}</option>
</select>
答案 2 :(得分:2)
您可以简单地使用$ event属性获取所选值和文本
html代码
<label>Select Market</label><br/>
<select class="user-preselect btn-add" style="width: 90%;height: 34px;"(change)="selectchange($event)">
<option value="0" selected="selected">ADD Market</option>
<option *ngFor="let country of Countrylist" [value]="country.id" >{{country.name}}</option>
</select><br/><br/>
打字稿代码
selectchange(args){
this.countryValue = args.target.value;
this.countryName = args.target.options[args.target.selectedIndex].text;
}
答案 3 :(得分:2)
您可以使用
<select class="form-control" (change)="onChange($event)">
</select>
然后在组件中
onChange($event){
let text = $event.target.options[$event.target.options.selectedIndex].text;
}
答案 4 :(得分:2)
如果您只想要标题值。
只需使用:
public onOptionsSelected(event) {
const value = event.target.value;
let id = event.target.options[event.target.options.selectedIndex].title;
alert(id);
}
答案 5 :(得分:1)
使用#reference变量,比其他任何答案都容易得多。
进行了大量实验,以使其正常工作。
<select #selectedSegment id="segment" name="segment" [(ngModel)]="entryForm.segmentId" type="text">
<option *ngFor="let segment of segments" value="{{segment.id}}">{{segment.segmentName}}</option>
</select>
<div *ngIf="selectedSegment.selectedIndex !== -1 && selectedSegment.options.item(selectedSegment.selectedIndex).text === 'Testing'">
{{ selectedSegment.options.item(selectedSegment.selectedIndex).text }}
</div>
答案 6 :(得分:1)
HTML:
Rook.py
.ts:
from tkinter import *
from classes.ChessBoard import *
from classes.Pieces import *
class Rook(Pieces):
def __init__(self, X, Y, color):
Pieces.__init__(self, X, Y, color)
self.img = PhotoImage(file=path + "T{}.gif".format(self.color[0].upper()))
self.board.create_image(self.coordinatX, self.coordinatY, anchor=NW, image=self.img)
答案 7 :(得分:1)
在 .ts 页中,输入以下代码。
(this.industries.filter((ele)=> {return ele.ID == this.form.controls ['Industry']。value}))[0]。名称
您将获得行业名称。
答案 8 :(得分:0)
进行以下更改
<强>模板强>
<form [formGroup]="test">
<div class="col-sm-6 form-group">
<label>Industry</label>
<select class="form-control select" formControlName="Industry">
<option *ngFor="let industry of industries" [ngValue]="industry.id">{{industry.id}} </option>
</select>
</div>
</form>
<pre>{{test.value | json}} </pre>
<强>组件强>
import { Component,OnInit } from '@angular/core';
import {FormArray, FormGroup, FormControl, Validators} from "@angular/forms";
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent implements OnInit {
name = 'Angular 5';
test:FormGroup;
industries = [{id:1},{id:2}];
ngOnInit(){
this.test = new FormGroup({
Industry:new FormControl('')
});
this.test.get('Industry').valueChanges.subscribe(data => console.log(data));
}
}
答案 9 :(得分:0)
尝试以下方法:
upload.component.html
<form [formGroup]="form">
<div class="col-sm-6 form-group">
<label>Industry</label>
<select class="form-control select"
formControlName="industry" (change)="onChange()">
<option value="">Please select Value</option>
<option *ngFor="let industry of industries"
[ngValue]="industry.id">{{ industry.name }}
</option>
</select>
</div>
</form>
upload.component.ts
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormControl } from "@angular/forms";
@Component({
selector: 'app-upload',
templateUrl: './upload.component.html',
styleUrls: [ './upload.component.css' ]
})
export class UploadComponent implements OnInit {
form: FormGroup;
select: FormControl;
industries = [
{id: 1, name: 'Mining'},
{id: 2, name: 'Information Technology'},
{id: 3, name: 'Entertainment'}
];
ngOnInit(){
this.createForm();
}
createForm() {
this.select = new FormControl('');
this.form = new FormGroup({
select: this.select
});
}
onChange() {
console.log(this.select.value);
# to get the name that was selected
const id = parseInt(this.select.value, 0);
const selected = this.industries
.filter(industry => industry.id === id)
.reduce(function(str: string, project) {
return str + project.name;
}, '');
console.log(selected.name);
}
}
希望这有助于及时。 :)
答案 10 :(得分:0)
您可以这样做:
在您的视图中
<select class="select" (change)="onChange($event.target.value)" [(ngModel)]="someObject.BoundItem" name="BoundItem" #BoundItem="ngModel">
<option *ngFor="let item of myObject" [value]="item.Id">{{item.displayiItem}}</option>
</select>
在您的组件
中onChange(selectedId: number)
{
this.selectedItem = this.myObject.find((x: any) => x.Id == selectedId);
console.log(this.selectedItem["displayItem");
}