如何在angularjs中创建动态表单?

时间:2016-01-28 15:03:53

标签: angularjs angularjs-directive angularjs-scope

我的错误说明:

第1步:我使用此指令附加模板。

第2步:现在我使用此范围函数删除此附加模板" removeMilestoneDiv"。

第3步:提交表格后。但是,我无法提交。

我想我已添加模板然后绑定此范围变量。但是,我已删除此模板,然后范围变量无法解除绑定。

创建添加里程碑的指令:

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++;
                });
            }
        }
    }]);

使用此功能删除里程碑:

$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); 
                    }
                }
            }
        },

1 个答案:

答案 0 :(得分:0)

创建一个数组:

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

创建一个范围功能

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

我的动态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 -->