question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add template reference variable for FormArrays

See original GitHub issue

One 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:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Totaticommented, Sep 7, 2020

We use this approach It’s almost the same, but we enforce the type in the *ngFor

0reactions
angular-automatic-lock-bot[bot]commented, Jul 26, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found