1

I'm developing an Angular 2 application with ASP.NET Core 2.0 and the Visual Studio 2017 Angular's Web application template.

I don't know how to get the selected value from a select when the user changes the selection.

This is the law.component.html:

<p *ngIf="!laws"><em>Loading...</em></p>
Legislaciones:&nbsp;
<select #l (change)="lawChanges(l.value)">
    <option></option>
    <option *ngFor="let law of laws" [ngValue]="law">{{law.name}}</option>
</select>

This is the law.component.ts:

import { Component, Inject, Output, EventEmitter } from '@angular/core';
import { Http, RequestOptions, Headers } from '@angular/http';

@Component({
    selector: 'law',
    templateUrl: './law.component.html'
})

export class LawComponent {
    public laws: Law[];

    @Output()
    change: EventEmitter<number> = new EventEmitter<number>();

    constructor(private http: Http,
        @Inject('BASE_URL') private baseUrl: string) {

        http.get(baseUrl + 'api/Law').subscribe(result => {
            this.laws = result.json() as Law[];
        }, error => console.error(error));
    }

    lawChanges(selectedLaw: Law) {
        console.log("selected law: " + selectedLaw.value + " " + selectedLaw.name + ".");
        this.change.emit(selectedLaw.value);
    }
}

interface Law {
    value: number;
    name: string;
}

At this what it generates:

<select>
    <option></option><!--bindings={"ng-reflect-ng-for-of": "[object Object],[object Object"}-->
    <option ng-reflect-ng-value="1">China</option>
    <option ng-reflect-ng-value="2">Korea</option>
    <option ng-reflect-ng-value="3">European</option>
</select>

This is the angular's version (ng --version):

    _                      _                 ____ _     ___
   / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
  / ? \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
 / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
/_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
               |___/
@angular/cli: 1.4.4
node: 6.11.3
os: win32 x64

This is the JSON returned by the controller:

[
    {
        "value": 1,
        "name": "China"
    },
    {
        "value": 2,
        "name": "Korea"
    },
    {
        "value": 3,
        "name": "European"
    }
]

At the console I get:

selected law: China .

It seems that value is empty.

How can I get the selected value? I think I'm doing something wrong when I define the select but I have found a lot of examples with the same syntax.

By the way, I've found how to do it from this SO answer.

VansFannel
  • 45,055
  • 107
  • 359
  • 626

3 Answers3

4

try to change the l.value to $event :

 <select (change)="lawChanges($event)">  

then

lawChanges(event : any) {
   let selectedLaw : any = event.target.value;
   console.log("selected law: " + selectedLaw.value + " " + selectedLaw.name + ".");
   this.change.emit(selectedLaw.value);

}

and in the <option> change the ngvalue to value:

<option *ngFor="let law of laws" [value]="law">{{law.name}}</option>

Hope it helps :)

Mohamed Ali RACHID
  • 3,245
  • 11
  • 22
1

This is how I have fix it:

<p *ngIf="!laws"><em>Loading...</em></p>
Legislaciones:&nbsp;
<select #selectedLaw (change)="lawChanges(selectedLaw.value)">
    <option></option>
    <option *ngFor="let law of laws" [value]="law.value">{{law.name}}</option>
</select>
VansFannel
  • 45,055
  • 107
  • 359
  • 626
0

you may try this code

//in html

<select #l="ngModel" [(ngModel)]="lawModel" name="lawName"  (change)="lawChanges(lawModel)">
    <option></option>
    <option *ngFor="let law of laws" [ngValue]="law">{{law.name}}</option>
</select>

// in ts declare lawModel as a variable;

lawModel:any;
Tushar Ghosh
  • 942
  • 1
  • 12
  • 18