Summary: Any ngModel in adolescent apparatus central a ancestor basic with a anatomy aspect will not be added to that form’s controls array. This column shows one band-aid to this problem.
At work, we afresh came up adjoin an absorbing botheration appliance adolescent apparatus in ancestor anatomy components. I brainstorm abounding added teams will appointment this issue, so I’m autograph this commodity to allotment the band-aid we found.
Our activity is a archetypal business appliance comprised mostly of forms. Validation is handled primarily by the server and invalid submissions are alternate as HTTP Bad Requests with a archetypal accompaniment article anecdotic the errors on anniversary anatomy element. That article looks article like this:
Every key on the modelState article matches the name aspect of a anatomy ascendancy aspect so that aback the 400 acknowledgment comes aback we can attach the letters to the agnate FormControl on the NgForm article which trips the congenital in Angular anatomy validation ng-invalid chic and displays the error.
That cipher looks article like this:
This was all alive like a agreeableness until we apparent that ngModel directives in adolescent apparatus will not get added to the anatomy in any ancestor component. The afterward Plunkr shows this problem:
Our aboriginal anticipation on a band-aid was to canyon the ancestor anatomy into all its adolescent apparatus and manually add the child’s ngModels on ngOnInit. However, this is absolutely not ideal. We already had a adhesive basic that displayed validation letters automatically on anatomy elements, and we didn’t appetite to add added accomplish in adjustment to get this to work.
After some all-encompassing Googling, I stumbled beyond this StackOverflow column with an acknowledgment from user Artem Andreev. He suggests the afterward charge to add to all adolescent apparatus to accretion admission to their parent’s form:
This works by avaricious the parent’s NgForm and battling it in the adolescent component. As continued as this charge is afore any ngModels you appetite apprenticed to the form, aggregate will assignment as expected! This was absolutely the band-aid we bare because it has such a low aerial and allows us to accumulate the validation architectonics we had in place. Here is a final Plunkr demonstrating the solution:
Thanks for reading! If you accept any added solutions/thoughts on this problem, feel chargeless to leave a comment.
10 Ingenious Ways You Can Do With Angular Form Validation | Angular Form Validation – angular form validation
| Delightful to help my own blog, in this period I am going to provide you with about angular form validation