Vignesh Vignesh - 1 year ago 78
Javascript Question

ng-repeat for tr to add rows dynamically.

<tr class="workTime" ng-repeat="worktime in doctor.shift" >
<td><select name="multipleSelect" class="form-control selectpicker"
id="multipleSelect" ng-model="doctor.shift.workDays" multiple>
<option ng-repeat="days in weekdays">{{days}}</option>
<td><input type="text" class="form-control" ng-model =
<td><input type="text" class="form-control" ng-model =
<td><span class="glyphicon **glyphicon-plus** glyph_size " ng-
click="addWorkTime()" aria-hidden="true "></span></td>

I am new to Angular. I would like to repeat the tr on click of the glyphicon-plus. I am not sure how to do this. I am not fetching any data. I just want to add a row with the respective fields on click of the glyphicon-plus. Can some please help me to resolve this. And if I have to write any script. What script do I have to write

Answer Source

OK, here's the sort answer:

var app = angular.module('main', []); 
app.controller('ctrl', function ($scope) {
    // create an object to hold the collection
    $ = {
        shift : {
        worktimes: []

    $scope.addWorkTime = function() {
        // add an element to the end of the array
      $ {
                workDay: 1,
            workFrom: '9:00',
            workTo: '22:00'
    // add an initial row - if there are no rows, there's no add button!

The long answer is that there's some funny stuff in your html that makes it clear you're just getting started with Angular. For starters, when you say this: ng-repeat="worktime in doctor.shift" ..."worktime" is a temporary variable created solely for iterating over the object or (more likely) collection. Therefore, in the repeated template, you'd want to bind to things like: "worktime.workFrom" You'll see that I tweaked your html and your data model to make it all work, while trying to guess at what you were trying to accomplish.

Next, for the options on your select box, the current way of doing it is with "ngOptions", and not "ngRepeat" :

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download