You can test it in this jsFiddle: HERE (better is to see on new jsFiddle, see EDIT part of this post)
I think there is a bug in AngularJS or at least not an expected result.
If i detach a form then re-append it, it's class ng-invalid
switch to ng-valid
on re-append it to the DOM. This has for consequence to enable the submit button of the form even data aren't valid.
Of course, i was expecting that validity status didn't switch.
I think it's a angular bug, but maybe a jquery one. I could use jquery to check on append if form was valid or not and then forcing form class but it's seems not working as a valid form get then the status of invalid. It's quite weird as i don't know any other workaround without using kind of data to saved status form before detaching it.
So anyone has already encountered this problem? Is there any method (if possible using AngularJS directive) to get rid of this bug?
PS: i need to detach form (and any other elements) in a single page web application to keep DOM as clean as possible.
EDIT
I've done a new jsFiddle that is illustrating more my problem, detaching content on internal site navigation: http://jsfiddle.net/EWVwa/
UPDATE
I come to this temporary solution (thanks to CaioToOn)
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's 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 need more testing regarding multiple inputs validation. I'll certainly updating answer when all tests will be done.