Generate a div element dynamically when an option is selected using AngularJS

I'm having trouble dynamically creating div elements based on the selected option value, but for some reason ng-repeat isn't working as expected. Can you help me figure out what I'm missing?

Here's the HTML snippet I'm using -

<!doctype html>
<html lang="en" ng-app="myApp">
<head>
    <meta charset="utf-8" />
    <title>Select Example - AngularJS</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular.min.js"></script>
    <script src="script.js"></script>
</head>
<body ng-controller="myCtrl">
    <select id="shiftnumbers"
            ng-model="event.shiftnumber"
            ng-options="timeslot.value as timeslot.name for timeslot in shiftnumbers"
            ng-init="event.shiftnumber = shiftnumbers[0].value"
            class="btn">
    </select>
    <div ng-repeat="event.shiftnumber">{{event.shiftnumber}}</div>
</body>
</html>

And here's my Script -

var myApp = angular.module("myApp", []);

myApp.controller("myCtrl", function($scope){

    $scope.shiftnumbers = [];
    for(var i=0; i< 23; i++) {
        $scope.shiftnumbers.push({name: i, value: i});
    }
});

Check out this PLNKR LINK for a live demo - http://plnkr.co/edit/UruYYbXrTgxqXadIb8A3?p=preview

Answer №1

Start by creating a new array called selectedShiftValues to hold the chosen values. Then, utilize ngRepeat to display them.

HTML

<select id="shiftvalues"
        ng-model="event.shiftvalue"
        ng-change="addShiftValue(event.shiftvalue)"
        ng-options="timeslot.value as timeslot.name for timeslot in shiftvalues"
        ng-init="event.shiftvalue = shiftvalues[0].value"
        class="btn">
</select>
<div ng-repeat="shiftvalue in selectedShiftValues">
  {{shiftvalue}}
</div>

Script

$scope.addShiftValue = function(value){
  $scope.selectedShiftValues = [];
  for(var i=0; i< value; i++) {
    $scope.selectedShiftValues.push(i);
  }
}

VIEW DEMO

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 HTML element failed to be inserted

Currently, I am involved in a project based on .NET Core for my organization. This project entails loading work orders from our SQL database using Entity Framework and then filtering them to display as markers on a map via the Google Maps API for our insta ...

Utilizing React and Material-UI to create an autocomplete feature for words within sentences that are not the first word

Looking to enable hashtag autocomplete on my webapp, where typing #h would display a menu with options like #hello, #hope, etc. Since I'm using material-ui extensively within the app, it would be convenient to utilize the autocomplete component for th ...

Encountering a ReferenceError message that says "readFile is not defined

Currently, I am in the process of learning Node.js and encountering an issue. When I type 'node .' in the terminal, I receive a ReferenceError: readFile is not defined message. Below is the code snippet: const express = require("express"); co ...

The request in Node.js was terminated

While using express and body-parser to transfer a large amount of data from one server to another, I encountered the following exception after some time: { "message": "request aborted", "code": "ECONNABORTED", "expected": 99010, "length": ...

Obtain the HTML of a Vue component and transmit it using an ajax POST request

I need to send an email with an HTML body using only ajax since I don't have access to the server code. Fortunately, the server has an API for sending emails. Currently, I have a dynamically rendered component called invoiceEmail. sendEmail () { ...

Positioning three squares with the same class adjacent to each other by utilizing absolute positioning

I've been pondering whether it's possible to align elements with the same class and position: absolute next to each other. After an hour of experimenting, I discovered a solution: http://jsfiddle.net/hv01ad1r/1/ However, when attempting to use d ...

Lots of questions arise when dealing with a combination of a restful back-end and Angular.js

Alright, it's time to dive into building a web application using Jboss and restEasy for the backend, with Angular.js as the chosen MVC front-end framework. This is uncharted territory for me, so here come my myriad questions: Should I utilize securi ...

Could you provide insight into the reason behind debounce being used for this specific binding?

function debounce(fn, delay) { var timer return function () { var context = this var args = arguments clearTimeout(timer) timer = setTimeout(function () { fn.apply(context, args) }, delay) ...

Tips for validating forms within a modal that includes tabs using angularJs

I have a modal form in angular with uib-tabset that includes tabs for forms A, B, and C within the footer section of the modal. However, I am facing an issue where I cannot access the forms to enable/disable the save button on the footer. The forms appear ...

Using Angular2 to Toggle Checkbox Field Enabled and Disabled

As I dynamically render checkbox and combobox fields, the following functionality is performed: If the checkbox field appears as selected based on the API response, then the combo box appears as enabled; otherwise, it would be disabled. If a user selects ...

Is there a method to ensure a sequence of jQuery ajax requests are processed in an orderly queue?

I have an array of data (arrData) that I need to loop through (10 elements), making an ajax request for each element. Currently, all the ajax calls are being sent in parallel, causing the server to receive 10 requests at once and respond with the data in n ...

Unusual occurrence while creating a unique identifier for a React component

I am working on creating a unique identification number for each React component, which will be assigned to the component upon mounting. Here is the approach I am taking: The callOnce function is used to ensure that a specific function is only executed on ...

javascript various backgrounds on click

I have implemented a list to allow users to select their top 3 choices, and I am using JavaScript to track these selections and change the background color accordingly. 1st selection -> Green background 2nd selection -> Yellow background 3rd sel ...

I'm encountering a npm error on Windows_NT 10.0.19042, does anyone know how to troubleshoot this issue?

After downgrading to [email protected], I encountered an error message that keeps popping up whenever I try to update npm or install new packages. What steps can I take to resolve this issue? npm ERR! Windows_NT 10.0.19042 npm ERR! argv "C:\ ...

Error: The script is unable to access the property "div" because it is undefined

I keep encountering this issue "Cannot read property 'div' of undefined" This is the snippet in question function validateData(){ for(let j = 0; j < 13; j++){ if(dataArr[j].data.textContent === resultSet[j].result.div. ...

What is the best way to retrieve the color of a WebElement when dealing with a complex CSS structure in Webdriver

I am currently trying to determine if the color of a specific page changes immediately after clicking on a button designed to alter the color of an element within that page. My goal is to extract the RGB value stated as rgb(183,168,168); in this particul ...

The iPhone header position switches when an HTML5 input type number is selected

I am currently working on a project that involves jQuery Mobile and PhoneGap. One issue I am encountering is when using the iPhone version, the header position changes when I click on an input type number. NOTE: When I focus on the input type number in my ...

The Vue v-on:click event listener seems to be unresponsive when applied to a

I've been attempting to utilize the on-click directive within a component, but for some reason, it doesn't seem to be functioning. Whenever I click on the component, nothing happens, even though I should see 'test clicked' in the consol ...

Printing Multiple Pages Using JavaScript and Cascading Style Sheets

Encountering difficulties displaying page numbers when printing multiple multipage reports Here is the provided HTML Format : <style type="text/css> body { counter-reset: report 1 page 0; } td.footer:after { counter-increment: page; content ...

Tips for uploading an image file from Django Admin to the HTML

If I have already used a for loop in my code, how should I write the image address if I don't want to use 'for'? Can I directly write something like '/img/1.jpg'? Here is the directory where my images are stored: This is my Djang ...