2013-12-19 99 views
4

我有一個AngularJS驗證的表單。
的問題是,我使用jQuery和AngularJS不添加這些元素到窗體的添加驗證輸入元素這種形式..
下面是一個例子:http://jsfiddle.net/ULEsy/AngularJS表單驗證,同時添加動態元素

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

在這個例子中,即使輸入字段無效(空 - 可以通過紅色邊框看到),整個表單有效。 有什麼幫助嗎?

+0

我沒有太多經驗的角度,但[這](https://github.com/angular/angular.js/issues/1404)可能是一個開始。 – Peter

+0

我已經看到了這個線程..區別在於Angular是將元素添加到窗體中的一個..在我的情況下,它是jquery .. –

回答

2

@Ephi我找到了解決這個問題的辦法。 顯然,您需要先將元素追加到DOM,然後才使用$ compile。另外,新元素需要一個名稱。

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

如果你想刪除一個元素,你應該刪除它,然後編譯它的父.. –