Introduction
A few months ago, I wrote an article based on Angular 2 for beginners and today in this article, we will learn about Angular 2 with TypeScript to bind dropdown and select value from dropdown. Angular dropdown is not any big deal; it's the same as Angular 1 but here there are some differences of approach. If you are a beginner in Angular, you need to go through the article "Getting started with Angular2 using Visual Studio code"
Create Angular application using TypeScript.
- Install Nodejs, NPM on your system. Resource https://nodejs.org/en/
- Install Visual Studio Code on your machine. Resource https://code.visualstudio.com/
- Use the latest version of NPM and VS Code.
Configure as per project requirement (component, HTML page, routing, module, etc....).
Step 3
Create service to get data from API and store in an object. If you are not using service to get data, just create a simple array list to store information.
Step 4
So far, I hope you have configured your application with all dependencies; if not, I would like to refer you to this article to create a simple architecture using system.js, config.js.
Create a function inside your component.ts (see the code given below). Now, I am going to explain some basic points carefully.
- import {
- Component,
- ViewEncapsulation
- } from '@angular/core';
- import {
- MyCodeService
- } from '../../../../_services';
- import {
- Router
- } from '@angular/router';
- @Component({
- selector: 'Home-component',
- templateUrl: './Home.component.html',
- encapsulation: ViewEncapsulation.None
- })
- export class HomeComponent {
- nameList: any;
- nameId: Number;
- constructor(private _myCodeService: MyCodeService) {
- this.getNameList();
- }
- getNameList() {
- this._myCodeService.getNameList().subscribe(data => {
- this.nameList = data;
- });
- }
- selectName() {
- alert(this.nameId);
- }
- }
- import { Component, ViewEncapsulation } from '@angular/core';
- import { MyCodeService } from '../../../../_services';
- import { Router } from '@angular/router';
- @Component({
- selector: 'Home-component',
- templateUrl: './Home.component.html',
- encapsulation: ViewEncapsulation.None
- })
- export class HomeComponent {
- nameList: any;
- data:any;
- nameId:Number;
- constructor( private _myCodeService : MyCodeService ) {
- this.getNameList();
- }
- getNameList()
- {
- this.nameList= this.data;
- }
- selectName()
- {
- alert(this.nameId);
- }
- this.data=[
- {
- "Id": 3,
- "Name": "Attorney Case"
- },
- {
- "Id": 1035,
- "Name": "bikesh appeal"
- },
- {
- "Id": 22,
- "Name": "BikeshAppeal"
- },
- {
- "Id": 20,
- "Name": "Case Info"
- },
- {
- "Id": 15,
- "Name": "Case Infoe"
- },
- {
- "Id": 11,
- "Name": "Case Prep"
- }
- ]
- }
Create an HTML page and dropdown element to bind data (see code given below). You can see in the given code, I am using <select> element. This element is used to iterate the value and populate in options.
- <div class="box-header with-border">
- <div class="form-group voffset row">
- <label for="" class="vertical-label col-sm-4 col-md-8 text-right"> Select Name</label>
- <div class="col-sm-8 col-md-4">
- <select id="Id" #Id="ngModel" class="hideLabel form-control" [(ngModel)]="nameId" name="Id" (change)="selectName();">
- <option selected="" value=""></option>
- <option [ngValue]="k.Id" *ngFor="let k of nameList">
- {{k.Name}}
- </option>
- </select>
- </div>
- </div>
- </div>
[(ngModel)]: is use to control data and pass selected value from View to component to View and View to component (Two way binding).

Ketan ShindePosted Apr 18, 2019, 10:04 AM
Not getting proper