Add template reference variable for FormArrays
See original GitHub issueOne can self-reference forms as so:
<form #thisForm="ngForm">
Yay I can see myself: {{ thisForm.value | json }}
</form>
However, there is no way to refer to FormArrays within the form. Thusly, we’re forced to do something like this:
<div formArrayName="sadArray">
<li *ngFor="let control of getFormControlsWithBurdensomeCode("saddAray"); let i=index">
<input [formControlName]="i">
</li>
</div>
// => ERROR: typo in your second reference to "sadArray", and now your app is broken.
// (I hope you didn't accidentally refer to a *different* FormArray; that's even worse!)
// => PAIN_IN_BUTT: You had to write code for fetching FormArray controls.
Now imagine the beauty of using a template reference variable:
<div formArrayName="happyArray" #thisArray="INSERT_FORM_ARRAY_DIRECTIVE_REFERENCE">
<li *ngFor="let control of thisArray.controls; let i=index">
<input [formControlName]="i">
</li>
</div>
// => Success: Your app worked perfectly. Little code, no typos. Way to go!
Benefits of template reference variables for FormArrays:
- IDE’s treat #templateVariables as typed variables, so they will help you out accordingly. Whereas “sadArray” was just a string, so you get no help, and you’re vulnerable to typos and bugs.
- getFormControlsWithBurdensomeCode(“yourFormArraylName”) is no longer necessary.
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
How to set unique template reference variables inside an ...
Your template reference variable is already unique because you use it inside embedded view scope: <div *ngFor="let person of attendeesList"> ...
Read more >Understanding template variables - Angular
Refer to a template variable anywhere in the component's template. ... A template input variable is a variable with a value that is...
Read more >How to Use Template Reference Variables in Angular
To create a template reference variable, locate the HTML element that you want to reference and then tag it like so: #myVarName ....
Read more >Angular Forms Guide: Template Driven and Reactive Forms
We are using to get a reference to the ngForm directive, which is implicitly applied to all HTML <form> elements by the Angular...
Read more >Testing Template Reference Variable in Angular-angular.js
What you can do is to use dispatchEvent in the following way: it('simple input test', fakeAsync(() => { const fundChangedSpy = spyOn(component, ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
We use this approach It’s almost the same, but we enforce the type in the *ngFor
This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.