0

I have a div containing another div which further contains three input elements. On one of the input element i am using angular number-only directive and on-change directive. I have a button outside of this div on click of whom this div which contains three input elements in cloned and appended to the main div using jquery. So in order to make the directive work i am recompiling the main directive using $compile function which is working fine. But the problem is that when that page is reloaded i need to fill the saved data for which i am again using the same clone functions to create another rows then jquery code to fill the dynamically created input elements but it is not showing the data in that elements. I have attached the code to crate dynamic elements and to fill those elements

var bootableId = '';
var dyanamicLocation = $('#divLocation').clone(true);
var createRemoveButton = $(dyanamicLocation).find('.removeDynamicDiv');
var Id = $('*[locIndex]').length;
bootableId = '#divLocation' + Id;
$(dyanamicLocation).attr('id', 'divLocation' + Id);
$(dyanamicLocation).find('#Starthours').attr('data-ng-model', 'Starthours' + Id);
$(dyanamicLocation).find('#Startmins').attr('data-ng-model', 'Startmins' + Id);
$(dyanamicLocation).find('#Endhours').attr('data-ng-model', 'Endhours' + Id);
$(dyanamicLocation).find('#Endmins').attr('data-ng-model', 'Endmins' + Id);
$(dyanamicLocation).find("#location").attr("ng-model", 'location' + Id);
$(dyanamicLocation).find("#locationDate").removeAttr("data-ng-model");
$(dyanamicLocation).find("#location").removeAttr("ng-model");

$(dyanamicLocation).find('#Starthours,#Startmins,#location,#Endhours,#Endmins').val('');
$(dyanamicLocation).find("#locationDate").attr("disabled", true);
$('#divtotLocations').append(dyanamicLocation);
if ($('*[locIndex]').length > 1) {
createRemoveButton.css('display', 'block');
createRemoveButton.on('click', function () { removeDynamicObjects(this); });
}
var rebootableElement = $(bootableId);
$compile(rebootableElement.contents())($scope);

and the HTML is

<div id="divtotLocations">
    <div id="divLocation" class="divLocation" locindex="0">
        <div class="row margin-top-10">
            <div class="col-md-8"><span id="locationError" style="display:block;color:red;" name="SpanMessageLocation"></span></div>
            <div class="col-md-4">
                <span removetype="location" class="btn btn-sm btn-default-small pull-right removeDynamicDiv" style="display:none;">REMOVE</span>
            </div>
        </div>
        <div class="row margin-top-20">
            <div class="col-md-4">
                <label class="text-label">ROOM: <span class="redText">*</span></label>
                <input id="location" name="location" ng-model="location" data-ng-keyup="chkLoctionExists($event)"  style="box-shadow: 0px 0px 0px 0px; " type="text" value="" class="text-field" required>
            </div>
            <div class="col-md-4">
                <label class="text-label">DATE: <span class="redText">*</span></label>
                <input id="locationDate" class="text-field datepicker calenderIcon" name="locationDate" type="text" data-ng-model="locationDate" eps-datepicker mindate="{{minDate}}" ng-readonly="true"/>
            </div>

            <div class="minwidth160 pull-left">
                <label class="text-label">START TIME: <span class="redText">*</span></label>
                <div style="float: left; margin-right: 2px;">
                    <input id="Starthours" class="text-content-reg startendtime" placeholder="HH" name="Starthours" type="text" ng-change="validateHours(this);" data-ng-model="Starthours" maxlength="2" numbers-only="numbers-only" />
                </div>
                <div style="float: left; margin-right: 2px;">
                    <input id="Startmins" class="text-content-reg startendtime" placeholder="MM" name="Startmins" type="text" ng-change="validateMinnute(this);" data-ng-model="Startmins" maxlength="2" numbers-only="numbers-only">
                </div>
                <div class="pull-left">
                    <select id="ddlStartAMPM" name="ddlStartAMPM" class="startendtimeselect selectgrey">
                        <option value="AM">AM</option>
                        <option value="PM">PM</option>
                    </select>
                </div>
            </div>
            <div class="minwidth160 pull-left">
                <label class="text-label">END TIME: <span class="redText">*</span></label>
                <div style="float: left; margin-right: 2px;">
                    <input id="Endhours" class="text-content-reg startendtime" name="Endhours" placeholder="HH" type="text" data-ng-model="Endhours" ng-change="validateHours(this);" maxlength="2" numbers-only="numbers-only" />
                </div>
                <div style="float: left; margin-right: 2px;">
                    <input id="Endmins" class="text-content-reg startendtime" name="Endmins" placeholder="MM" type="text" data-ng-model="Endmins" ng-change="validateMinnute(this);" maxlength="2" numbers-only="numbers-only" />
                </div>
                <div class="pull-left">
                    <select id="ddlEndAMPM" name="ddlEndAMPM" class="startendtimeselect selectgrey">
                        <option value="AM">AM</option>
                        <option value="PM">PM</option>
                    </select>
                </div>
            </div>
        </div>
        <hr class="hr-dashed">
    </div>
</div>
Alberto I.N.J.
  • 1,855
  • 14
  • 19
Shukshum
  • 53
  • 1
  • 6
  • there is little HTML removed but this html is contained in two divs and can be understood.Actually input elements are shown that needs to get data filled – Shukshum Sep 21 '15 at 11:40
  • 2
    You have a mess on your hands by using jQuery to do what you should be doing with `ng-repeat` driven by your data model. Your event handlers won't work using `this` which has different context in angular controller. Best suggestion is remove all of that jQuery and rewrite using angular best practices – charlietfl Sep 21 '15 at 11:43
  • 3
    Also strongly suggest you thoroughly read: [thinking-in-angularjs-if-i-have-a-jquery-background](http://stackoverflow.com/questions/14994391/thinking-in-angularjs-if-i-have-a-jquery-background) – charlietfl Sep 21 '15 at 11:47
  • but we are creating elements dynamically on click of button so that was done using jquery and everything is working just fine except that when the page is reloaded it is not showing data in dynamically created elements – Shukshum Sep 21 '15 at 11:51
  • 1
    Fine but it is even easier to create the same elements using angular without having to do any dom manipulation yourself ... you work with the data model and let angular create the dom. A simple `push()` to data model array will create that whole row for you – charlietfl Sep 21 '15 at 11:54

0 Answers0