Using ng-repeat to dynamically generate rows in a table

<div class="workTime-row" ng-repeat="worktime in doctor.shifts">
    <input type="text" class="form-control" ng-model="worktime.workDays" multiple>
    <input type="text" class="form-control" ng-model="worktime.workFrom">
    <input type="text" class="form-control" ng-model="worktime.workTo">
    <span class="glyphicon glyphicon-plus glyph_size" ng-click="addWorkTime()" aria-hidden="true"></span>
</div>

I am exploring Angular and need help creating a dynamic form that can add new rows on click of the plus icon. I do not have data to fetch, just want to dynamically add rows with specific fields upon clicking the plus icon. Any tips or scripts would be much appreciated.

Answer №1

Alright, let me give you the concise answer:

let application = angular.module('main', []); 
application.controller('controller', function ($scope) {
    // creating an object to store the collection
    $scope.doctor = {
        shift : {
            worktimes: []
        }
    };

    $scope.addWorkTime = function() {
        // adding an element to the end of the array
        $scope.doctor.shift.worktimes.push( {
            workDay: 1,
            workFrom: '9:00',
            workTo: '22:00'
        });
    };
    // adding an initial row - without rows, there's no add button!
    $scope.addWorkTime();
});

Check out this jsfiddle link for a visual representation.

The detailed explanation reveals that there are some issues in your html code, indicating that you're just beginning with Angular. When you use: ng-repeat="worktime in doctor.shift" ..."worktime" is a temporary variable used only for looping through the object or collection. So, in the looped template, you should bind to properties like: "worktime.workFrom". I made adjustments to your html and data model to ensure everything functions correctly and attempted to understand your intended outcome.

Furthermore, for the options in your select box, it's best to utilize "ngOptions" instead of "ngRepeat" : Refer to this documentation for more details.

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

Comparing the size of an array property in Mongoose with another property's size

Is it feasible to retrieve all documents containing an array property with a size greater or less than a specified number property? We are assuming that the numberProperty within the schema is of type Number and the array holds ObjectIds. For example: Co ...

NodeJs and the Power of Event Emitters

As a newcomer to Nodejs, I am diving into learning from various code snippets that I have come across on the internet. My current curiosity lies within a basic question regarding a chat application code snippet. Let's take a look at the code snippet ...

Exploring touch events and input focusing on mobile devices using JavaScript

Recently, I integrated a JS touch mapping function into one of my pages sourced from Stack Overflow. This was necessary to enable my jQuery draggables to work on iOS Safari, as drag and drop functionality was not functioning without this mapping. Here is ...

Combining two arrays of objects using unique keys in Angular 8

There are two Arrays of Objects, Arr1 and Arr2. The goal is to merge these two arrays based on their 'id' values. If Arr1 does not contain any object with the same id as in Arr2, then that particular object node should be pushed into Arr1. How ca ...

Step-by-step guide on retrieving JSONP data from Angular service within view by utilizing expressions

I have been developing an Angular application that retrieves data from the Strava API. While experimenting with $http.get, I realized that separating the logic into a dedicated service would be more organized, allowing my controller to simply call the serv ...

Using Browser.wait() in Protractor to wait for a specific ID while the class changes

Looking to implement browser.wait() specifically for waiting on a particular element. Need some guidance with this task. Situation -> When a filter div is clicked, a hidden div becomes visible. Need to wait until it appears. Before Click - <div i ...

Which is the better choice for developing a progressive web app - Django or Wagtail?

I am currently in the process of creating a website, and I have a question about progressive web apps. Are they compatible with all devices or only monitors? ...

Retrieve the HTML content starting from the nth tag and beyond

I am currently working on a table... <table class="col-xs-12"> <thead class="testhead col-xs-12"> <tr class="col-xs-12" id="row1" style="color: white;"> <th>0</th> ...

How can I implement a jQuery popup that prompts users to log in or register if they are not currently authenticated?

My JavaScript code includes jQuery and AJAX functionality for a specific action. Whenever a user id is not provided, and there isn't one stored in the session, I aim to prompt the user with a dialog box asking them to either register or log in. Could ...

Trigger Chrome Extension Background Page Using a Message

Currently, I am in the process of developing my initial chrome extension and focusing on establishing message passing and background.js functionality. However, it seems like my background.js file is not being triggered or receiving any messages. I am seeki ...

Retrieve an array of specific column values from an array of objects using Typescript

How can I extract an array from an array of objects? Data var result1 = [ {id:1, name:'Sandra', type:'user', username:'sandra'}, {id:2, name:'John', type:'admin', username:'johnny2'}, ...

Converting JSON date format while deducting one day

JSON is returning the date in this format: /Date(1412101800000)/ However, when I convert it to a normal date format, it ends up being one day less. The code I am using is: var dateFormat = new Date(parseInt(obj['DATEOFJOINING'].substr(6)) ...

Straining data in text input fields using bootstrap

Looking for a way to filter data with bootstrap/jquery without using tables, just div rows and columns. Check out an example of how my form looks Here's a snippet of my code: <div class="row"> <div class="col-md-4"> <input class= ...

Learn the steps to designing a responsive class using Angular2's ngClass feature

Imagine a scenario where the object models in my cloud Array include a weight key: return [ { term: '1994', weight: 0 }, { term: '2017', weight: 0 }, { term: '89th', ...

The dynamic text feature in my Angular Material gridlist functions perfectly in CodePen, however, it fails to work when

I have enhanced the Angular material gridlist demo by incorporating static and dynamic texts into the grid tiles. While this modification works flawlessly on Codepen (see my provided link), it does not function properly when deployed on my server. The sta ...

"Expand your list in AngularJS by automatically adding more items if the ng-repeat count is

I am struggling with this section of code. It currently shows a box with the first four linked resource images. However, I need it to display additional images if there are less than four. I tried looking for a solution, but couldn't find one. <di ...

Sometimes the last column might not be visible in an HTML table within a collapsed Bootstrap element while using Chrome browser

I've encountered a strange issue when trying to wrap an HTML table inside a collapsing div element using Bootstrap. The last column is not consistently displayed in Chrome (44.0.2403.107 m) depending on the window width, but it works fine in Firefox. ...

Challenges when testing Angular controllers using Jasmine - modular problem

Recently, I made the decision to explore testing my Angular code using Jasmine. While everything seems to work fine without specific dependencies, I encountered problems when there are dependencies involved. For instance, in our project we use controllers. ...

file system mistakenly saves document in incorrect directory

For some reason when I try to use writefile, it is writing the file outside of the ./events folder. I am attempting to use fs like this: fs.writeFile("./level.json", JSON.stringify(storage), (err) => { console.log(err) xp ...

clicking on a link with the symbol "#" in the href attribute in Firefox prevents the setter

Good morning everyone, It's early Monday morning and I'm having trouble understanding why this particular line works in Internet Explorer but not in Firefox. <a class="button" href="#" onclick="setMaintenanceMode(false);">disable</a> ...