简体   繁体   English

AngularJS在添加动态元素时进行表单验证

[英]AngularJS form validation while adding dynamic elements

I have a form with AngularJS validation. 我有一个AngularJS验证表单。
The problem is that I add input elements to this form using Jquery and AngularJS doesn't add those elements into the form's validation.. 问题是我使用Jquery向此表单添加输入元素,而AngularJS不会将这些元素添加到表单的验证中。
Here's an example: http://jsfiddle.net/ULEsy/ 这是一个例子: http//jsfiddle.net/ULEsy/

var input = $compile($('<input type="text" ng-model="textinput" required />'))($scope);
$("#someform").append(input);

In the example, even though the input field is not valid (empty - can be seen by the red border), the entire form is valid. 在示例中,即使输入字段无效(空 - 可以通过红色边框看到),整个表单也是有效的。 Any help? 有帮助吗?

@Ephi and I found a solution for this problem. @Ephi和我找到了解决这个问题的方法。 Apparently, you need to first append the element to the DOM, and only then use $compile. 显然,您需要先将元素附加到DOM,然后才使用$ compile。 Also, the new element needs a name. 此外,新元素需要一个名称。

See here 看到这里

angular.module("app", []).controller('someController', function($scope, $compile) {   
    $scope.add = function() {
        var input = $('<input type="text" ng-model="textinput" name="x" required />');
        $("#someform4").append(input);    
        $compile(input)($scope);
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM