AngularJS表单验证,同时添加动态元素 [英] AngularJS form validation while adding dynamic elements
问题描述
我有AngularJS验证的一种形式。结果
问题是,我想补充输入元素使用jQuery和AngularJS不添加这些元素到窗体的验证这种形式..结果
这里有一个例子: http://jsfiddle.net/ULEsy/
I have a form with AngularJS validation.
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..
Here's an example: 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 ,我找到了解决办法针对此问题。
显然,你需要的元素首先追加到DOM,然后才使用$编译。此外,新的元素需要一个名称。
@Ephi and I found a solution for this problem. Apparently, you need to first append the element to the DOM, and only then use $compile. Also, the new element needs a name.
请参阅这里
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);
}
});
这篇关于AngularJS表单验证,同时添加动态元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!