I am trying to validate some form fields which are given to me from a backend endpoint...
So basically the input elements are dynamically created inside
Try my custom directive:
myApp.directive("dynamicName",function($compile){
return {
restrict:"A",
terminal:true,
priority:1000,
link:function(scope,element,attrs){
element.attr('name', scope.$eval(attrs.dynamicName));
element.removeAttr("dynamic-name");
$compile(element)(scope);
}
};
});
Use it:
DEMO
Explanation of the problem:
By default, input elements using ngModelController (ng-model) call FormController.$addControl when they are linked to register itself and expose a property on the FormController with the name property of the input which is {{ field.name }} in this case. Therefore, even though the control is registered but you don't have exposed properties on FormController with named email, firstName, you only have {{ field.name }} referencing the last input item
Explanation of the solution:
In this solution, I created a custom directive to replace the {{ field.name }} with the correct name at runtime.
For more information why I have to use terminal:true, and priority:1000, check out this discussion: Add directives from directive in AngularJS