4

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?

2
  • I don't have much experience with angular, but this might be a start. Commented Dec 19, 2013 at 13:34
  • I already saw this thread.. The difference is that there Angular is the one that is adding the elements to the form.. And in my case it is jquery.. Commented Dec 19, 2013 at 13:41

1 Answer 1

2

@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.

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);
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

FYI if you want to remove an element you should remove it and then compile its parent..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.