You can check this out in this jsFiddle: HERE (better to see the new jsFiddle, see the EDIT section of this post)
I think there is a bug in AngularJS, or at least not the expected result. If I disconnect the form, then re-add it, the ng-invalid class will switch to ng-valid to re-add it to the DOM. This leads to the fact that the submit button is even invalid. Of course, I expected that the status of reality will not switch.
I think this is an angular error, but maybe jquery. I could use jquery to check if the form was valid or not, and then it forces the form class, but doesn't seem to work as a valid form, and then has an invalid status. This is rather strange since I donβt know any other workaround without using data for the saved state form before disconnecting it.
So, has anyone already encountered this problem? Is there any method (if possible, using the AngularJS directive) to get rid of this error?
PS: I need to separate the form (and any other elements) in a single page web application so that the DOM is as clean as possible.
EDIT
I made a new jsFiddle that illustrates my problem, separating content from internal site navigation: http://jsfiddle.net/EWVwa/
UPDATE
I came to this workaround (thanks to CaioToOn)
http://plnkr.co/edit/KIgMz2
var app = angular.module('plunker', []); app.controller('MainCtrl', function($scope) { $scope.name = 'World'; }); app.directive('customValidation', function() { return { require: ['ngModel', '^?form'], link: function(scope, element, attr, ctrls) { console.log(ctrls); var ngModelCtrl = ctrls[0], formCtrl = ctrls[1]; ngModelCtrl.$parsers.push(function(viewValue) { if (viewValue === 'test') { ngModelCtrl.$setValidity('name', true); formCtrl.$setValidity('name', true); return viewValue; } else { ngModelCtrl.$setValidity('name', false); formCtrl.$setValidity('name', false); return undefined; } }); // custom event element.bind('$append', function() { formCtrl && formCtrl.$addControl(ngModelCtrl); /*** TEST for to keep form validation status ***/ formCtrl.$setValidity('name', ngModelCtrl.$valid); //ngModelCtrl.$setValidity('name', ngModelCtrl.$valid); console.log(formCtrl.$valid); }); //binding on element, not scope. element.bind('$destroy', function() { console.log("gone haven"); }); } }; });
This requires more tests related to checking multiple inputs. I will definitely update the answer when all tests are completed.