Pra*_*vin 8 angular angular-reactive-forms
我想在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);
});
Run Code Online (Sandbox Code Playgroud)
我正在使用Reactive的formControlName属性.
请建议回顾选定文本选择控件
实现这一目标的最简单方法是:从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>
Run Code Online (Sandbox Code Playgroud)
打字稿
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 = '';
}
Run Code Online (Sandbox Code Playgroud)
模型类
export class ListItemSimplified {
id: string;
name: string;
}
Run Code Online (Sandbox Code Playgroud)
小智 6
getSelectedOptionText(event: Event) {
let selectedOptions = event.target['options'];
let selectedIndex = selectedOptions.selectedIndex;
let selectElementText = selectedOptions[selectedIndex].text;
console.log(selectElementText)
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
您可以使用
<select class="form-control" (change)="onChange($event)">
</select>
Run Code Online (Sandbox Code Playgroud)
然后在组件中
onChange($event){
let text = $event.target.options[$event.target.options.selectedIndex].text;
}
Run Code Online (Sandbox Code Playgroud)
进行以下更改
模板
<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>
Run Code Online (Sandbox Code Playgroud)
成分
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));
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
27099 次 |
| 最近记录: |