The ng-option feature has taken over as the new default option

My select tag looks like this:

 <select ng-model="vm.fooModel"
         ng-options="item.id as item.name for item in vm.fooOptions" >
     <option value="none">Select</option>
 </select>

The problem arises when angular 1.6 renders it, as it removes my default option and adds an empty one

     <option value="?" selected="selected"></option>

Initially, "vm.fooModel" is set to "none". This used to work fine in Angular 1.5.

Answer №1

To populate options in a <select> element, you can utilize the ng-repeat method:

angular.module("app",[])
.controller("ctrl", function(){
  var vm = this;
  vm.fooModel="none";
  vm.fooOptions=[
    {id: 1, name:"one"},
    {id: 2, name:"two"},
  ];
})
<script src="//unpkg.com/angular/angular.js"></script>
<body ng-app="app" ng-controller="ctrl as vm">
    <select ng-model="vm.fooModel">
      <option value="none" disabled>Select</option>
      <option ng-repeat="item in vm.fooOptions" ng-value="item.id">
        {{item.name}}
      </option>   
    </select>
    value={{vm.fooModel}}
 </body>

For further details, visit AngularJS <select> Directive API Reference - Using ngRepeat to generate select options.

Answer №2

Include a default option in an array of choices within the controller.

angular.module("app", [])
  .controller("ctrl", function() {
    var vm = this;
    vm.fooModel = "none";
    vm.fooOptions = [{
        id: "none",
        name: "default"
      },
      {
        id: 1,
        name: "one"
      },
      {
        id: 2,
        name: "two"
      },
    ];
  })
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<body ng-app="app" ng-controller="ctrl as vm">
  <select ng-model="vm.fooModel" ng-options="item.id as item.name for item in vm.fooOptions">
  </select>

  Selection: {{vm.fooModel}}
</body>

Answer №3

<option ng-repeat="element in vm.choices track by element.key" ng-value="element.value">

Implement the track by feature within the ng-repeat loop

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is the video failing due to excessive adjustments of the video's currentTime?

Utilizing both user drag event and keypresses to adjust the position in an HTML5 video element, I am updating the video time with the following line of code: video.currentTime = toTime; Additionally, I am updating a canvas based on the video's posit ...

Ways to deactivate the submit button using Cookies, sessions, or local storage

Although cookies and storage can be deleted, they can still help prevent many human spammers who are unaware of this. How can I temporarily disable the form submit button on a specific page for just one day? I want to create a code that saves "Submitted" ...

Complete loading of iframe content on Internet Explorer versions 8 and 9

Having an issue with the full loading of a page in IE8-9. I need to perform certain actions after the content within a div is fully loaded, but it seems that in IE8-9, the page fails to load completely. This is causing problems as my actions are dependent ...

Modifying a Field's Value by Referring to a Different Field

I am working on developing a form that includes a dropdown menu for changing the aircraft type. Additionally, I want to incorporate another field named "Registrations" which will automatically update the available registration options based on the selected ...

Determine the value of an object by iterating through its keys

UPDATE: (for clarification) I currently have a table named modelCoa +----+----------+-------+--------------------+ | id | id_parent| code | name | +----+----------+-------+--------------------+ | 1 | 0 | 1 | asset ...

There seems to be an issue with the functionality of flatlist and scrollView

I am new to working with react-native. I have a screen where I am using four flatlists, but the last flatlist at the bottom of the screen is not scrolling. I added a scrollView and it worked fine, but now I am getting this error message: VirtualizedLists ...

React hitting recursion limit due to excessive shouldComponentUpdate checks

I'm currently developing a real-time updating dashboard using React. The data for the dashboard components is fetched via an Ajax call and then passed to each component successfully. However, I encountered an issue with one specific component that re ...

Leveraging Next.js to efficiently handle multiple asynchronous requests with NextResponse

I have developed a login/signup application using NextJS. When attempting to log in, the logic in my route.ts file sends requests to a MongoDB database to check if the user exists and if the password is correct. However, instead of receiving the expected 4 ...

Creating a custom 404 page with intricate dynamic nested paths in React Router 6: a complete guide

How can I create a custom Not Found page for complex dynamic nested routes in React Router 6? For instance, I have dynamic routes set up for categories and product pages like: site.com/jeans site.com/jeans/qazXzx123122 To display the data on these catego ...

Concise way to add or insert an element into an array, at a specific property of an object

I am working with an object of the ObjectOfArrays type: type ObjectOfArrays = { [id: string]: Array<string> }; Throughout my code, I need to add strings to the correct array based on the id. Currently, I am using the following approach: if (id i ...

Redraw only a specific offspring

Currently, I am in the process of constructing a dynamic form and my component hierarchy looks like this:- App Caseform DynamicFormBuilder Context.Provider Patients Patient key = "patient_1" ComponentCrea ...

Warning: The core schema has detected an unknown property `color` for the component or system `undefined` in Aframe + Vuejs. This issue was flagged within 10 milliseconds in

I am facing some challenges trying to integrate Aframe and vuejs seamlessly, as the console is displaying warning messages. It seems like Aframe is validating the attribute values before vue has a chance to modify them. Warning messages core:schema:warn ...

Fetching JSON data from an external URL using AngularJS

Check out this URL that shows JSON data in the browser: I attempted to store the data in a variable with the following code: $http.get('http://api.geosvc.com/rest/US/84606/nearby?apikey=4ff687893a7b468cb520b3c4e967c4da&d=20&pt=PostalCode& ...

Creating artistic masterpieces on a digital canvas with the use of touch

After conducting some research, I am struggling to find a solution that allows touch screen users to draw on the canvas tag using raw JavaScript. Despite my best efforts, I keep hitting a dead end and my canvas remains unresponsive. The canvas is located ...

When utilizing React, I generated an HTML page with a distinct .js file, however, encountered two unexpected errors

Update : Gratitude to everyone who has helped me in tackling this issue. One user suggested using a web server, but my intention was to find a solution using just a single HTML and JS file. Even though I tried following the steps from a similar question o ...

improper rendering of highcharts

Receiving data in JSON format as shown below: <?php $monthlyParticipation='[{"project_title":"test project 44","project_ref_id":"113","amount":"13000.00","months":"Feb"},{"project_title":"sdsdsd","project_ref_id":"112","amount":"50000.00","months ...

AngularJS filter that retrieves only values that have an exact match

In my AngularJS application, I have a checkbox that acts as a filter: Here is the data being used: app.controller('listdata', function($scope, $http) { $scope.users = [{ "name": "pravin", "queue": [{ "number": "456", "st ...

Unable to eliminate UI component by clicking in Angular

I have been experimenting with different examples and attempted a few methods, but I am struggling to remove items from the UI level. I attempted to use the *ngFor directive, however, it seems to only remove <span> elements and not the <button> ...

Verify whether a certain key exists within the gun.js file

Suppose I need to verify whether a specific entry exists in the database before adding it. I attempted the following: gun.get('demograph').once((data, key) => { console.log("realtime updates 1:", data); }); However, I only receive ...

Create a variety of tables populated with numerous hyperlinks within each table on a webpage

I have a website where I need to display multiple tables with different data, along with appropriate links within the table cells. Plan: Each table on the webpage represents a different school subject, showcasing performance metrics such as SAT scores, fi ...