Angular consistently defaults to selecting the initial option within a select element

Here's my current approach with the select element. I am not utilizing the ng-options feature due to the need for disabling options and operating on angular 1.3.x where the 'disable by' feature is apparently in version 1.4.

<select class="checkout" 
        ng-model="user.deliveryTime" 
        ng-change="timeUpdate = true"
        ng-class="{'selected': user.deliveryTime}" >
  <option value="" disabled selected>Delivery Slot </option>
  <option ng-repeat="deliveryTime in intervals" 
          ng-disabled="deliveryTime.tooLate" value="{{deliveryTime.time}}">
       {{deliveryTime.time}}
   </option>
</select>

This code snippet explains how to preselect a specific value within the select element:

config.deliveryTimes(function(m){
  $scope.intervals = m;
  $scope.user.deliveryTime = $scope.intervals[2].time;
});

The config.deliveryTimes(callback) function essentially initiates an HTTP request and handles the response accordingly.

Despite setting the option to the third value of the array explicitly, the first option always gets selected. Even hard-coding the options within the controller doesn't resolve this issue.

Any suggestions or guidance would be greatly appreciated!

Answer №1

Make sure to remove the selected attribute from the default option and replace it with

ng-selected="user.deliveryTime === deliveryTime.time"
:

<select class="checkout" 
        ng-model="user.deliveryTime" 
        ng-change="timeUpdate  = true"
        ng-class="{'selected' : user.deliveryTime}" >
  <option value="" disabled>Choose a Delivery Time</option>
  <option ng-repeat="deliveryTime in intervals" 
          ng-disabled="deliveryTime.tooLate" value="{{deliveryTime.time}}"
          ng-selected="user.deliveryTime === deliveryTime.time">
       {{deliveryTime.time}}
   </option>
</select>

Answer №2

By utilizing ng-value instead of value within the option tag, you can link the selected option to the options established in the ng-model (user.deliverytime)

<select class="checkout" 
    ng-model="user.deliveryTime" 
    ng-change="timeUpdate  = true"
    ng-class="{'selected' : user.deliveryTime}" >
<option value="" disabled>Delivery Slot </option>
<option ng-repeat = "deliveryTime in intervals" 
        ng-disabled="deliveryTime.tooLate" ng-value="deliveryTime.time"
        >
     {{deliveryTime.time}}
 </option>

http://plnkr.co/edit/8hKIXwK5i35lczWEtuCw?p=preview

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

Having trouble with basic authorization for Facebook API using JavaScript?

I am having an issue with my source code. When I run it, I receive an alert that says "No Response" and then the Facebook page displays an error message stating "An error occurred with MYAPP. Please try again later" <div id="fb-root"></div> & ...

Issue with Bootstrap Nav Dropdown in Yeoman Gulp-Angular configuration

I recently built an Angular App using the Swiip/generator-gulp-angular framework and ran into some issues when trying to implement a bootstrap nav dropdown menu. After searching for a solution, I came across this helpful Stack Overflow thread: Bootstrap D ...

Best practice in AngularJS involves calling a function defined in the controller and exposed through $scope on a model also defined in the controller and exposed through $scope

Imagine you have a controller with a model exposed on its scope that can be manipulated in a template. There is also a function in the controller that acts on that model, which is invoked by a click handler in the template. The question arises: when and wh ...

Error encountered: Unexpected '<' token when trying to deploy

Trying to deploy a React app with React Router on a Node/Express server to Heroku, but encountering the following error... 'Uncaught SyntaxError: Unexpected token <' Suspecting the issue may lie in the 'catch all' route in the Expr ...

button that takes you back to the top of a mobile website

I want to customize the Scroll To Top button so that it only appears once the user begins scrolling down, rather than always being visible even when at the top of the page. Just a heads up, I don't have much experience with JavaScript, so I might need ...

When displaying content within an iframe, toggle the visibility of div elements

I'm attempting to toggle the visibility of certain page elements based on whether the page is loaded in an iframe. <script>this.top.location !== this.location && (this.top.location = this.location);</script> The above code succes ...

Utilizing jQuery: Understanding how to access the selected object within .each loop

Is there a way to obtain a reference to the element that was matched? Using 'this' does not seem to work. For instance, in this scenario I am attempting to display an alert with the value of the elements. //How can I ensure that 'this&apos ...

Design a custom Bootstrap dropdown using an input[type="text"] element

After exploring the Bootstrap dropdown example here, I realized that for my particular scenario, it would be more beneficial to have an input field (type="text") instead of a button. This way, I can display the selected option from the dropdown. Is there ...

Navigating with React Router version 6 while implementing protected routes through the Context API

I have a setup that includes the following components: App.js import AppContext from './context/AppContext.js'; import router from './routes/routes.js'; function App() { const [token, setToken] = useState(""); f ...

Personalized JSON response type for AJAX requests

Do you think it's possible to achieve this? I have an idea to create a unique dataType called "json/rows". This new dataType would parse the server output text and manipulate it in some way before passing it to the success function. What do you think ...

Having trouble running the script, chrome error with message passing?

I've hit a roadblock while working on my Chrome extension and could use some assistance. The main issue I'm facing is getting the script to run when activated by the user through an on/off switch in the popup window. It seems like there might be ...

What sets apart genuine user interactions from programmatically generated actions?

Working with webkit notifications on Chrome has presented a challenge. The window.webkitNotifications.requestPermission method must be called from a user action, such as a click. Attempting to call it at any other time will not have any effect and will not ...

Is it possible to use async/await with the React setState method?

Seeking clarification on the use of async/await in React setState method. I previously believed it only functioned with Promises, but I have not found any definitive documentation supporting this. Any assistance would be greatly appreciated! In my applica ...

Security vulnerability found in version 1.2.9 due to insecure local URL

I am facing an issue with the code below: <span ng-include="getHeaderTemplate()"></span> The function getHeaderTemplate() is returning "templates/header/prospection.html" However, I am encountering the following error message: Error: [$sce ...

Downloading files from a WebAPI using AngularJS

Searching for a way to download a file from the server using Web API and AngularJS? Below is an example of code used in an API controller. When accessing the API through a browser, the file can be successfully downloaded. However, implementing this functio ...

Retrieve JSON data from an external link and showcase it within a div, unfortunately encountering an issue with the functionality

Encountering the error message "XMLHttpRequest cannot load . No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '' is therefore not allowed access" Check out the plunker link for more information: http ...

The POST Request will exclusively provide the ID in JSON format without including any other data

Trying to make a POST request from Postman to Mongo Database. Currently, only the ID of the object is being returned and the data is not displaying. Data Model: const mongoose = require("mongoose"); const categorySchema = mongoose.Schema({ name: String, ...

Pausing for an asynchronous operation to complete prior to submitting a form

This particular issue presents a challenge. Below is my jQuery code snippet: /** * Updating websites */ $('.website').on('blur', function () { var websiteId = this.id; console.log(websiteId); var website = this.value; ...

Having difficulties executing AJAX requests on Codepen.io

My CodePen project that I created 6 months ago is no longer functioning properly. The project includes an Ajax call to retrieve data using jQuery.ajax(). When attempting to load the content from CodePen via https, even though the ajax request is through h ...

Having trouble with a JavaScript function and tag that don't seem to be working in Chrome, but function well in Mozilla Firefox. Can anyone provide assistance with

Below is the code for creating two dropdowns: one for "Property Type" and the second for "Property Sub Type". This code works fine in Firefox but not in Chrome. When selecting "residential" as the property type, only residential values will appear in the ...