"Incorporate a new dropdown menu and then append the selected item to an array within

I am trying to enhance my user interface by adding two new select boxes (from and until) dynamically using ng-click, but I am facing challenges in binding these selects to my array in the scope.

Here is the HTML code snippet:

<div ng-repeat="time in availability.times" class="calendar--availability--time">
    <select ng-model="availability.times.time.from[$index]">
     <option>...</option>
    </select>
    <select ng-model="availability.times.time.to[$index]">
     <option>...</option>
    </select>
</div>
<button class="button--secondary margin__top" ng-click="addNewTime()"><span>Add another slot</span></button>

This is the JavaScript/AngularJS code snippet:

$scope.availability = {
    times: [
        time: {from: '09:00', to: '21:00'}
    ]
}

$scope.addNewTime = function() {

    var newTime = {time: { from: '', until: ''}};
    $scope.availability.times.push(newTime);

}

Answer №1

It appears there are a few issues in the HTML markup and JavaScript code provided.

The time value should not be inside the array brackets. It seems like you only need from and to. The correct format should be:

times: [
    {from: '09:00', to: '21:00'}
]

In your addNewTime function, make sure to update the object structure and fix any typos. For example:

var newTime = {from: '', to: ''};

Also, ensure that the ng-model binding in the HTML references the appropriate variables. Instead of using $index, simply use time created by ng-repeat. Update it like this:

<select ng-model="time.from">

You can see a working example of these changes in this fiddle: http://jsfiddle.net/etnfwdw7/.

Lastly, I recommend getting familiar with developer tools in browsers as they can help identify such errors more efficiently.

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

The ClearInterval() function does not take effect instantly

I've implemented a Carousel with an auto-toggle feature using the setInterval() function to switch between tabs every 4 seconds. However, I now need to stop this automatic toggling when a specific tab is clicked. You can find the HTML and jQuery for ...

Having numerous profiles open could lead to ultimately controlling just one - selenium

I'm developing a unique application that empowers users to automate a process across multiple chrome profiles simultaneously. However, I am encountering an issue where the actions performed on each profile are only affecting one. Within my main scrip ...

Three.js: Loading DAE Files

Having trouble loading a DAE file in Three.js. Can anyone offer some assistance? I have the model.dae file in the same directory as my index.html, but when I load the page, it's just showing a black screen. I originally had an FBX file and exported ...

Initiate an ajax request only when there are pre-existing ajax requests in the queue

Setting up the Form I've created a form that utilizes checkboxes to determine a selected number on a page: https://i.sstatic.net/BNQB0.png When a checkbox is selected, it triggers a call to a controller that saves the selection and returns the upda ...

Controlling international shortcuts for numerous npm packages

Within my root folder, I have 3 npm projects organized in a more complex structure than the following example (using webpack, JS frameworks, etc.), but for simplicity sake, here is the layout: root ├── root_index.js ├── package.json ├── p ...

Guide on navigating through various HTML pages with distinct parameters using Node.js (Express server)

Seeking assistance with a Node.js server that receives an ID as a query parameter. Each time a client connects with different parameters, I aim to serve them a unique HTML page containing a simple UI with 2 dynamic arrays. Everything seems to be working co ...

Steps to retrieve a Leaflet GeoJSON feature that includes a specified latitude and longitude coordinate

In my project, I have implemented a LeafletJS map that includes a GeoJSON layer consisting of multiple polygons. The goal is to develop a script that can identify the polygon in the GeoJSON layer where a user-provided latitude/longitude coordinate falls an ...

Updating the state of an object within a mapping function

I've been struggling with this issue for two days now. Despite my efforts to find a solution online, I am still stuck and starting to believe that I might be missing something. The main functionality of the app is to click a button and watch an apple ...

Unable to access serialized select2 value in asp.net mvc controller

I'm facing a challenge with serializing and passing multiple controls inside a div to a controller via AJAX in my view. One of the fields, SchoolType, is a select2 multi-select dropdown. Model : public class SchoolModel { public string StudentN ...

What is the process for implementing token authentication within headers using an interceptor, especially when the token may be null?

In my Angular13 application with OAuth authentication, I am encountering issues when trying to add the token for all services. I have been unsuccessful in managing the undefined token. Despite trying various techniques to retrieve the token, I always enco ...

Fetching external data in a Cordova application from a remote server

I have encountered multiple questions similar to mine, but none of them have been able to solve my issue. Currently, I am developing a Cordova app for testing on Android and iOS platforms. My goal is to retrieve data in JSON format from my webserver using ...

The setInterval function in JavaScript fails to update the result

I have successfully implemented a countdown function that is working as expected. // Set the date we're counting down to var countDownDate = new Date("<?= $stop_datejs ?>"); // Update the count down every 1 second var x ...

Grab the source attribute of the <img> tag (identified by class) across numerous HTML pages

Here is an example: A website has the URL , where xxxx represents an integer between 1 and 1935. On each page, there may be an <img class="thumbnail" src="Images\Robots\{robotname}.png">. However, not all pages have th ...

When there is no data in the request body, ExpressJS will display it as empty

A big part of my tech stack includes a website with an express server up and running. The website allows users to insert their username and password. Upon clicking the login button, the server receives a request with the username and password packed as a J ...

The textarea linked to ng-model fails to submit when utilizing a wysiwyg editor

I encountered an issue while trying to create a form using ng-submit. The form includes a textarea that utilizes the WYSIWYG editor Trumbowyg. Although all other form data is submitted successfully, the content of this particular textarea is not being incl ...

Chai spy does not recognize sinon stubbed functions when verifying function calls

I am working with two asynchronous functions that return bluebird promises: Async1: function() { return new Promise(function(resolve, reject) { execute(query) .then(function(resp) { resolve(resp); }) .catch(function(err) { ...

Making changes to a variable within a Service

Hey there! I've been stuck on this code all day and could really use some help. I have a simple textbox that interacts with a controller to update a variable inside a service (which will eventually handle data from elsewhere). Currently, I can retri ...

What is the most effective way to ensure that a child component only executes when a link is clicked on the Vue component?

There are two components in my code The first component, which is the parent component, looks like this : <template> <ul class="list-group"> <li v-for="item in invoices" class="list-group-item"> <div class="ro ...

Implementing the if/shim binding in Knockout.js

In my project, I am looking to develop a unique custom binding that functions similarly to the if binding. However, instead of completely removing the element from view, I want it to be replaced with another element of equal height whenever it needs to be ...

Tips for displaying an uploaded image using the Valums file uploader

I recently implemented the Andrew Valums File Uploader on my website and it's functioning as expected. I am now looking to modify it so that instead of displaying just the uploaded filename and size, it will show the uploaded picture directly in the b ...