How do I get the value from the select option in Angular 4?
I want to assign it to a new variable in the component.ts file. I've tried this but outputs nothing.
Can you also do it using [(ngModel)]?
component.html
<form class="form-inline" (ngSubmit)="HelloCorp(f)" #f="ngForm">
<div class="select">
<select class="form-control col-lg-8" #corporation required>
<option *ngFor="let corporation of corporations" [value]="corporationObj">{{corporation.corp_name}}</option>
</select>
<button type="submit" class="btn btn-primary manage">Submit</button>
</div>
</form>
component.ts
HelloCorp() {
const corporationObj = corporation.value;
}
This question is related to
angular
angular-directive
angular4-forms
export class MyComponent implements OnInit {_x000D_
_x000D_
items: any[] = [_x000D_
{ id: 1, name: 'one' },_x000D_
{ id: 2, name: 'two' },_x000D_
{ id: 3, name: 'three' },_x000D_
{ id: 4, name: 'four' },_x000D_
{ id: 5, name: 'five' },_x000D_
{ id: 6, name: 'six' }_x000D_
];_x000D_
selected: number = 1;_x000D_
_x000D_
constructor() {_x000D_
}_x000D_
_x000D_
ngOnInit() {_x000D_
}_x000D_
_x000D_
selectOption(id: number) {_x000D_
//getted from event_x000D_
console.log(id);_x000D_
//getted from binding_x000D_
console.log(this.selected)_x000D_
}_x000D_
_x000D_
}
_x000D_
<div>_x000D_
<select (change)="selectOption($event.target.value)"_x000D_
[(ngModel)]="selected">_x000D_
<option [value]="item.id" *ngFor="let item of items">{{item.name}}</option>_x000D_
</select>_x000D_
</div>
_x000D_
This is very simple actually.
Please notice that I'm
I. adding name="selectedCorp" to your select opening tag, and
II. changing your [value]="corporationObj" to [value]="corporation", which is consistent with the corporation in your *ngFor="let corporation of corporations" statement:
<form class="form-inline" (ngSubmit)="HelloCorp(f)" #f="ngForm">
<div class="select">
<select class="form-control col-lg-8" #corporation name="selectedCorp" required>
<option *ngFor="let corporation of corporations" [value]="corporation">{{corporation.corp_name}}</option>
</select>
<button type="submit" class="btn btn-primary manage">Submit</button>
</div>
</form>
And then in your .ts file, you just do the following:
HelloCorp(form: NgForm) {
const corporationObj = form.value.selectedCorp;
}
and the const corporationObj now is the selected corporation object, which will include all the properties of the corporation class you have defined.
NOTE:
In the html code, by the statement [value]="corporation", the corporation (from *ngFor="let corporation of corporations") is bound to [value], and the name property will get the value.
Since the name is "selectedCorp", you can get the actual value by getting "form.value.selectedCorp" in your .ts file.
By the way, actually you don't need the "#corporation" in your "select" opening tag.
As a general (see Stackblitz here: https://stackblitz.com/edit/angular-gh2rjx):
HTML
<select [(ngModel)]="selectedOption">
<option *ngFor="let o of options">
{{o.name}}
</option>
</select>
<button (click)="print()">Click me</button>
<p>Selected option: {{ selectedOption }}</p>
<p>Button output: {{ printedOption }}</p>
Typescript
export class AppComponent {
selectedOption: string;
printedOption: string;
options = [
{ name: "option1", value: 1 },
{ name: "option2", value: 2 }
]
print() {
this.printedOption = this.selectedOption;
}
}
In your specific case you can use ngModel like this:
<form class="form-inline" (ngSubmit)="HelloCorp()">
<div class="select">
<select [(ngModel)]="corporationObj" class="form-control col-lg-8" #corporation required>
<option *ngFor="let corporation of corporations"></option>
</select>
<button type="submit" class="btn btn-primary manage">Submit</button>
</div>
</form>
HelloCorp() {
console.log("My input: ", corporationObj);
}
HTML code
<form class="form-inline" (ngSubmit)="HelloCorp(modelName)">
<div class="select">
<select class="form-control col-lg-8" [(ngModel)]="modelName" required>
<option *ngFor="let corporation of corporations" [ngValue]="corporation">
{{corporation.corp_name}}
</option>
</select>
<button type="submit" class="btn btn-primary manage">Submit</button>
</div>
</form>
Component code
HelloCorp(corporation) {
var corporationObj = corporation.value;
}
You just need to put [(ngModel)]
on your select element:
<select class="form-control col-lg-8" #corporation required [(ngModel)]="selectedValue">
//in html file
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="name">Country</label>
<select class="form-control" formControlName="country" (change)="onCountryChange($event.target.value)">
<option disabled selected value [ngValue]="null"> -- Select Country -- </option>
<option *ngFor="let country of countries" [value]="country.id">{{country.name}}</option>
<div *ngIf="isEdit">
<option></option>
</div>
</select>
</div>
</div>
</div>
<div class="help-block" *ngIf="studentForm.get('country').invalid && studentForm.get('country').touched">
<div *ngIf="studentForm.get('country').errors.required">*country is required</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="name">State</label>
<select class="form-control" formControlName="state" (change)="onStateChange($event.target.value)">
<option disabled selected value [ngValue]="null"> -- Select State -- </option>
<option *ngFor="let state of states" [value]="state.id">{{state.state_name}}</option>
</select>
</div>
</div>
</div>
<div class="help-block" *ngIf="studentForm.get('state').invalid && studentForm.get('state').touched">
<div *ngIf="studentForm.get('state').errors.required">*state is enter code hererequired</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="name">City</label>
<select class="form-control" formControlName="city">
<option disabled selected value [ngValue]="null"> -- Select City -- </option>
<option *ngFor="let city of cities" [value]="city.id" >{{city.city_name}}</option>
</select>
</div>
</div>
</div>
<div class="help-block" *ngIf="studentForm.get('city').invalid && studentForm.get('city').touched">
<div *ngIf="studentForm.get('city').errors.required">*city is required</div>
</div>
//then in component
onCountryChange(countryId:number){
this.studentServive.getSelectedState(countryId).subscribe(resData=>{
this.states = resData;
});
}
onStateChange(stateId:number){
this.studentServive.getSelectedCity(stateId).subscribe(resData=>{
this.cities = resData;
});
}`enter code here`
Source: Stackoverflow.com