动态渲染元素角形

时间:2017-03-17 17:10:44

标签: angularjs

您好我是基于bootstrap typeahead选择的数据类型动态添加元素。因为总是删除html元素并添加新的问题。表单错误集合仍然保留前一个错误并显示单个html元素的多重错误。请帮助这里有什么问题。

var addDynamicControl = function (fieldInformation) {

                   if (angular.element("#" + scope.advanceSearchItem.searchItemIndex).length) {
                       angular.element("#" + scope.advanceSearchItem.searchItemIndex).remove();
                       if (ctrl.advanceSearchForm[fieldInformation.SearchCriteriaName])
                           ctrl.advanceSearchForm[fieldInformation.SearchCriteriaName].$setValidity("required", false);
                   }
                   var angularForm = angular.element("#" + scope.formName);
                   var result = angularForm.append(getTemplateByType(fieldInformation));
                   $compile(result)(scope);
               };

enter image description here

1 个答案:

答案 0 :(得分:0)

在插入表单元素之前,应该清除typeahead。请尝试以下

var addDynamicControl = function(fieldInformation) {

    if (angular.element("#" + scope.advanceSearchItem.searchItemIndex).length) {
        angular.element("#" + scope.advanceSearchItem.searchItemIndex).remove();
        if (ctrl.advanceSearchForm[fieldInformation.SearchCriteriaName])
            ctrl.advanceSearchForm[fieldInformation.SearchCriteriaName].$setValidity("required", false);
    }
    $timeout(function() {
        angular.element('.typeahead')
            .typeahead('val', '');
    }, 0);

    var angularForm = angular.element("#" + scope.formName);

    var result = angularForm.append(getTemplateByType(fieldInformation));
    $compile(result)(scope);
};