Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VS2017 using the .Net Core 2 with Angular 5 template.

How do you get the value for the selected drop-down option.

Typescript file looks like this:

import { Component } from '@angular/core';

@Component({
  selector: 'app-sku-registration-component',
  templateUrl: './sku-registration.component.html'
})
export class SkuRegistrationComponent {

  catalogs = [
    { catalogname: "Macy's", catalogcode: "100" },
    { catalogname: "Sears", catalogcode: "200" },
    { catalogname: "JCPenny", catalogcode: "300" },
    { catalogname: "LL Bean", catalogcode: "B5000" }
  ];

}


and the html file like this:

HTML
<div class="container">
  <form #skuRegistrationForm="ngForm">
    
    <div class="form-group">
      <label for="power">Catalog</label>
      <select class="form-control" id="catalog"
              required
              [(ngModel)]="catalogs" name="catalogselector">
        <option *ngFor="let cat of catalogs" [ngValue]="cat">{{cat.catalogname + "(" + cat.catalogcode + ")"}}</option>
      </select>
    </div>
    <div>Selected catalog code: {{cat.catalogcode}}</div>
    <button (click)="incrementsku-registration()">SUBMIT</button>
  </form>
</div>


I can't get the following line to display the selected catalog code:

<div>Selected catalog code: {{cat.catalogcode}}</div>


I need to get the catalog code so I can use it to filter a dependent drop-down.

What I have tried:

I've tried changing [ngValue] to [value]

and I've tried several variations of the following:

{{cat.catalogcode}}
{{catalogs}}
{{catalogs?.catalogcode}}
{{catalogcode}}
Posted
Updated 22-Feb-19 16:58pm

1 solution

It should look like this,

<select [(ngModel)]="selected" name="status" placeholder="select" (ngModelChange)="onOptionsSelected()">
        <option *ngFor="let catalog of catalogs" [ngValue]="catalog">{{catalog.catalogname + "(" + catalog.catalogcode + ")"}}</option>
    </select>
<div>
<div>Selected catalog code: {{selected.catalogcode}}</div>
</div>


https://stackblitz.com/edit/angular-select-example-ngfor-nnarvg?file=src/app/app.component.html
 
Share this answer
 
Comments
littleGreenDude 25-Feb-19 9:17am    
Thank you for the feedback. Your solution works. One small edit though. Your link points back to this code project page and not to the stackblitz page.

Also for completeness (for anybody else who looks at this) could you include the typescript code in your answer?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900