DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Zone-Free Angular: Unlocking High-Performance Change Detection With Signals and Modern Reactivity
  • When Angular APIs Return 200 but the Frontend Is Already Failing Users
  • Why Angular Performance Problems Are Often Backend Problems
  • Faster Releases With DevOps: Java Microservices and Angular UI in CI/CD

Trending

  • When a Coding Agent Fixes the Test Instead of the Bug
  • After the AI Commit: What Eight Retrospectives Taught Me About Working With Coding Agents
  • Event Sourcing in Distributed Systems: Architecture, Consistency, and Fault-Tolerant Projections
  • Building IoT Time-Series Applications With Java and Apache IoTDB

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Frameworks
  4. Tutorial: Why Angular Dropdown Automatically Adds an Empty Value

Tutorial: Why Angular Dropdown Automatically Adds an Empty Value

We look at why Angular automatically populates a dropdown list with an empty value and how you can get around this in your code.

By 
Amar Srivastava
·
Sep. 21, 18 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
65.9K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Angular Select Has an Empty Value

Often, I'm faced with this problem while implementing a select (dropdown) in Angular. Here, I have tried to walk through why Angular adds an empty value at the beginning of your select list.

Let’s start you have code like this.

<select class="form-control" ng-model="selectedName_noinit" ng-options="employee.id as employee.name for employee in employees"></select>

$scope.employees  is defined as an array in a .js file that has the properties id and name.

This might generate something like:

<select class="form-control" ng-model="selectedName_noinit" ng-options="employee.id as employee.name for employee in employees">
            <option value="?" selected="selected"></option>
            <option value="0">Name1</option>
            <option value="1">Name2</option>
            <option value="2">Name3</option>
</select>

The question arises: Why do we get an option with a value and no text inside?

It’s because your model, selectedName, isn’t initialized with a valid value or, in this case, isn’t initialized at all. In order to not select a default actual value, Angular generates an empty option and selects it by default

How to Fix This

There are two ways to fix such common issues

For example:

<select class="form-control" ng-model="selectedName" ng-options="employee.id as employee.name for employee in employees" ng-init=""></select>

In this code, we specify an initial value for selectedName. On the ng-init we assign the value 0 as selected value for the dropdown.

<select class="form-control" ng-model="selectedName" ng-options="employee.id as employee.name for employee in employees" ng-init="">
            <option value="0" selected="selected">Name1</option>
            <option value="1">Name2</option>
            <option value="2">Name3</option>
</select>

Or, we can use the following line of JavaScript:

$scope.selectedName = 0;

We can define the initial value of selectedName in the controller, so it will generate this:

<select class="form-control" ng-model="selectedName" ng-options="employee.id as employee.name for employee in employees">
            <option value="0" selected="selected">Name1</option>
            <option value="1">Name2</option>
            <option value="2">Name3</option>
</select>

Note that ng-init allows functions. So you could call a function, initSelect(), which could wait for another value to initialize selectedName.

You can find a JSFiddle link for this article here and can play with the code.

Happy coding!


If you enjoyed this article and want to learn more about Angular, check out our compendium of tutorials and articles from JS to 8.

AngularJS

Opinions expressed by DZone contributors are their own.

Related

  • Zone-Free Angular: Unlocking High-Performance Change Detection With Signals and Modern Reactivity
  • When Angular APIs Return 200 but the Frontend Is Already Failing Users
  • Why Angular Performance Problems Are Often Backend Problems
  • Faster Releases With DevOps: Java Microservices and Angular UI in CI/CD

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook