Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

My error description:

step 1: I am appending template using this directive.

step 2: Now I remove this appended template using this scope function "removeMilestoneDiv".

step 3: After submit my form. But, I can't submitted.

I think I have add template then bind this scope variable. But, i have remove this template then scope variable is can't unbind.

Create directive for add milestone:

app.directive('addMilestone', ['$compile', function ($compile) { // inject $compile service as dependency
        return {
            restrict: 'A',
            link: function (scope, element, attrs) {
                // click on the button to add new input field
                element.find('a').bind('click', function () {

                    // I'm using Angular syntax. Using jQuery will have the same effect
                    // Create input element
                    // var input = angular.element('<div id="scope.mileStoneId_'+ scope.mileStoneCounter +'" class="form">Milestone - '+scope.mileStoneCounter+'</div>');

                    var input = angular.element(''+
                                '<div  class="card bg-white" id="mileStoneDiv_'+ scope.mileStoneCounter +'">'+
                                '<div class="card-header" ng-bind="\'project.ADD_MILESTONE\' | translate"></div>'+
                                '<a href="javascript:void(0)" class="removebtn glyphicon glyphicon-remove form-control-feedback" ng-click="removeMilestoneDiv('+ scope.mileStoneCounter +',0,0)"></a>'+
                                '<div class="card-block m-b-0">'+
                                    '<div compile-template class="form-group">'+
                                        '<label class="col-sm-2 control-label"><span ng-bind="\'project.COMPANY_MILESTONE_ID\' | translate"></span></label>'+
                                        '<div class="col-sm-5">'+
                                            '<input type="text" class="form-control" ng-model="company_milestone_id['+ scope.mileStoneCounter +']" ng-maxlength="100" name="company_milestone_id_'+ scope.mileStoneCounter +'" required>'+
                                            '<div ng-messages="frmProjectAdd[\'company_milestone_id_'+scope.mileStoneCounter+'\'].$error" role="alert" class="help-block has-error">'+
                                                '<span ng-message="required" class="help-block has-error">Company Milestone Id is required.</span>'+
                                                '<span class="help-block has-error" ng-message="maxlength">Maximum 100 characters allowed!</span>'+
                                            '</div>'+
                                        '</div>'+
                                    '</div>'+
                                    '<div compile-template class="form-group">'+
                                        '<label class="col-sm-2 control-label" ng-bind="\'project.MILESTONE_NAME\' | translate"></label>'+
                                        '<div class="col-sm-5">'+
                                            '<input type="text" ng-maxlength="100" class="form-control" ng-model="milestone_name['+ scope.mileStoneCounter +']" name="milestone_name_'+ scope.mileStoneCounter +'" required>'+
                                            '<div ng-messages="frmProjectAdd[\'milestone_name_'+scope.mileStoneCounter+'\'].$error" class="has-error login-error">'+
                                                '<span ng-message="required" class="help-block has-error">Milestone Name is required.</span>'+
                                            '</div>'+
                                        '</div>'+
                                    '</div>'+
                                    '<div compile-special-template class="form-group">'+
                                        '<label class="col-sm-2 control-label" ng-bind="\'project.SELECT_EMPLOYEE\' | translate"></label>'+
                                        '<div class="col-sm-10">'+
                                            '<select style="min-width:250px;" ui-select2 name="select_new_employee_'+scope.mileStoneCounter+'" ng-model="selectEmployee['+ scope.mileStoneCounter +']" data-placeholder="Select Employee" ng-required="true" multiple>'+
                                                '<option ng-repeat="manager in managerList" value="{{manager.id}}">{{manager.username}}</option>'+
                                            '</select>'+
                                        '</div>'+
                                        '<div ng-messages="frmProjectAdd[\'select_new_employee_'+scope.mileStoneCounter+'\'].$error" class="has-error login-error">'+
                                            '<span ng-message="required" class="help-block has-error">Employee is required.</span>'+
                                        '</div>'+
                                    '</div>'+    
                                '</div>');

                    // Compile the HTML and assign to scope
                    var compile = $compile(input)(scope);

                    // Append input to div
                    $('#milestoneHtml').append(input);

                    // Increment the counter for the next input to be added
                    scope.mileStoneCounter++;
                });
            }
        }
    }]);

Remove milestone using this function:

$scope.removeMilestoneDiv = function(key, id, flag) {

            var confirmVal = confirm("Are you sure you want to delete this Milestone?");
            if(confirmVal) {
                $('#mileStoneDiv_'+key).remove();
                if(flag == 1){
                    if(id != 0) {
                        $scope.deletedMilestoneIds.push(id); 
                    }
                }
            }
        },
share|improve this question
    
try to avoid using $ in controller & directive, you can use ngClick, ngIf ngShow to manipulate DOM – MarkoCen Jan 28 at 15:17

Create One Array:

$scope.mileStoneCounterNew = [{
    'countId':1,
    'company_milestone_id':'',
    'milestone_name':'',
    'milestone_start_date':'',
    'milestone_end_date':'',
    'milestone_completion_date':'',
    'selectEmployee':'',
}];

Create one scope function

$scope.milestoneHtmlAdd = function() {    
    $scope.mileStoneCounter++;
    var counterObj = {countId: $scope.mileStoneCounter };
    $scope.mileStoneCounterNew.push(counterObj);
};

My Dynamic HTML

<!-- Add Milestone - Start -->
        <div class="row" ng-repeat="(counterKey, counterValue) in mileStoneCounterNew" id="milestone_display_id_{{counterValue.countId}}">
            <div class="card bg-white mt50">
                <div class="card-header"><span ng-bind="'project.ADD_MILESTONE' | translate"></span></div>
                <a ng-if="counterKey > 0" href="javascript:void(0)" class="removebtn glyphicon glyphicon-remove form-control-feedback" ng-click="milestoneDivRemove(counterKey,0,0)"></a>
                <div class="card-block m-b">
                    <!--Milestone sub section-->
                    <!-- Company Milestone Id -->
                    <div class="form-group">
                        <label class="col-sm-2 control-label"><span ng-bind="'project.COMPANY_MILESTONE_ID' | translate"></span></label>
                        <div class="col-sm-5">
                            <input type="text" class="form-control" ng-model="counterValue.company_milestone_id" ng-maxlength="100" name="company_milestone_id_{{counterValue.countId}}" required>

                            <div ng-if="frmProjectAdd.$submitted || frmProjectAdd['company_milestone_id_'+counterValue.countId].$touched" ng-messages="frmProjectAdd['company_milestone_id_'+counterValue.countId].$error" role="alert" class="help-block has-error">
                                <span ng-message="required" class="help-block has-error">Company Milestone Id is required.</span>
                                <span class="help-block has-error" ng-message="maxlength">Maximum 100 characters allowed!</span>
                            </div>
                        </div>
                    </div>
                    <!-- Milestone name -->
                    <div class="form-group">
                        <label class="col-sm-2 control-label"><span ng-bind="'project.MILESTONE_NAME' | translate"></span></label>
                        <div class="col-sm-5">
                            <input type="text" class="form-control" ng-model="counterValue.milestone_name" ng-maxlength="100" name="milestone_name_{{counterValue.countId}}" required>

                            <div ng-if="frmProjectAdd.$submitted || frmProjectAdd['milestone_name_'+counterValue.countId].$touched" ng-messages="frmProjectAdd['milestone_name_'+counterValue.countId].$error" role="alert" class="help-block has-error">
                                <span ng-message="required" class="help-block has-error">Milestone Name is required.</span>
                                <span class="help-block has-error" ng-message="maxlength">Maximum 100 characters allowed!</span>
                            </div>
                        </div>
                    </div>
                    <!-- Milestone Start Date -->
                    <div class="form-group">
                        <label class="col-sm-2 control-label"><span ng-bind="'project.MILESTONE_START_DATE' | translate"></span></label>
                        <div class="col-sm-3">
                            <div class="input-group">
                                <input name="milestone_start_date_{{counterValue.countId}}" type="text" uib-datepicker-popup="MM/dd/yyyy" ng-model="counterValue.milestone_start_date" is-open="dpOpened['milestone_start_date_'+counterValue.countId]" ng-click="milestone_start_date[counterValue.countId].open = true" max-date="maxDate" datepicker-options="dateOptions"  close-text="Close" ng-required="true" class="form-control" 
                                min-date="minDate" ng-change="set_min_milestone_end_Date(milestone_start_date[counterValue.countId])"
                                disabled>
                                <span class="input-group-addon" ng-click="open($event,'milestone_start_date_{{counterValue.countId}}')"><i class="fa fa-calendar"></i></span>
                            </div>
                            <div ng-if="frmProjectAdd.$submitted || frmProjectAdd['milestone_start_date_'+counterValue.countId].$touched" ng-messages="frmProjectAdd['milestone_start_date_'+counterValue.countId].$error" role="alert" class="help-block has-error">
                                <span ng-message="required" class="help-block has-error">Milestone Start Date is required.</span>
                            </div>
                        </div>
                    </div>
                    <!-- Milestone End Date -->
                    <div class="form-group">
                        <label class="col-sm-2 control-label"><span ng-bind="'project.MILESTONE_END_DATE' | translate"></span></label>
                        <div class="col-sm-3">
                            <div class="input-group">
                                <input name="milestone_end_date_{{counterValue.countId}}" type="text" uib-datepicker-popup="MM/dd/yyyy" ng-model="counterValue.milestone_end_date" is-open="dpOpened['milestone_end_date_'+counterValue.countId]" ng-click="milestone_end_date[counterValue.countId].open = true" max-date="maxDate" datepicker-options="dateOptions"  close-text="Close" ng-required="true" class="form-control"
                                min-date="milestone_end_minDate"  ng-change="set_min_milestone_completed_Date(milestone_end_date[counterValue.countId])"
                                disabled>
                                <span class="input-group-addon" ng-click="open($event,'milestone_end_date_{{counterValue.countId}}')"><i class="fa fa-calendar"></i></span>
                                <div ng-if="frmProjectAdd.$submitted || frmProjectAdd['milestone_end_date_'+counterValue.countId].$touched" ng-messages="frmProjectAdd['milestone_end_date_'+counterValue.countId].$error" role="alert" class="help-block has-error">
                                    <span ng-message="required" class="help-block has-error">Milestone End Date is required.</span>
                                </div>
                            </div>
                        </div>
                    </div>
                    <!-- Milestone Completion Date -->
                    <div class="form-group">
                        <label class="col-sm-2 control-label"><span ng-bind="'project.MILESTONE_COMPLETION_DATE' | translate"></span></label>
                        <div class="col-sm-3">
                            <div class="input-group">
                                <input type="text" uib-datepicker-popup="MM/dd/yyyy" ng-model="counterValue.milestone_completion_date" is-open="dpOpened['milestone_completion_date_'+counterValue.countId]" ng-click="milestone_completion_date[counterValue.countId].open = true" max-date="maxDate" datepicker-options="dateOptions"  close-text="Close" class="form-control"
                                min-date="milestone_completed_minDate" disabled>
                                <span class="input-group-addon" ng-click="open($event,'milestone_completion_date_{{counterValue.countId}}')"><i class="fa fa-calendar"></i></span>
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label"><span ng-bind="'project.SELECT_EMPLOYEE' | translate"></span></label>
                        <div class="col-sm-10">
                            <select name="select_employee_{{counterValue.countId}}" ui-select2 ng-model="counterValue.selectEmployee" data-placeholder="Select Employee" ng-required="true" multiple>
                                <option ng-repeat="manager in managerList" value="{{manager.id}}">{{manager.username}}</option>
                            </select>
                            <div ng-if="frmProjectAdd.$submitted || frmProjectAdd['select_employee_'+counterValue.countId].$touched" ng-messages="frmProjectAdd['select_employee_'+counterValue.countId].$error" role="alert" class="help-block has-error">
                                <span ng-message="required" class="help-block has-error">Employee is required.</span>
                            </div>
                        </div>
                    </div>
                    <!-- Milestone sub section -->
                </div>
            </div>
        </div>
        <!-- Add Milestone End -->
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.