7

What is the scope of the template reference variable in Angular?

I can not find a clear answer to my question in documentation. Even though empirically I can say that the whole template can access the template reference variable.

1 Is it the case? Is the template reference variable guaranteed to be accessible in the whole template?

2 Or is the template reference variable accessible only in the child and sibling elements of the element which is referenced by the template reference variable?

Over here I found the following statement:

Use template variables to refer to elements — The newHero template variable refers to the <input> element. You can reference newHero from any sibling or child of the <input> element.

Does it mean that only the 2 is guaranteed by Angular?

2
  • You can refer anywhere in the component's template... else if you declare inside a *ngFor. In this case, the variable is used only inside the *ngFor. - In ts you can get all the elements with this template using @ViewChildren- Commented Jun 5, 2020 at 11:11
  • @Eliseo, could you, please, provide a source where you have learned about this? Thank you. Commented Jun 8, 2020 at 11:02

3 Answers 3

6

Template reference variable could be accessed from anywhere in the template as long as the corresponding element is present in the DOM.

For eg. you cannot access a template ref variable inside an <ng-template> element if it is not yet rendered.

<ng-template #templateOne let-message let-showAdditional="show">
  <div #templateDiv>
    One <br />
  </div>
</ng-template>

<ng-container *ngIf="templateDiv">
  Got template div
</ng-container>

Here Angular will throw an error

Property 'templateDiv' does not exist on type 'SomeComponent'.

because the element that is referenced by the variable is not present in the DOM.


Regarding your point 2.

You can reference newHero from any sibling or child of the element.

They were referring to the specific tutorial. Notice it didn't say

You can reference newHero only from any sibling or child of the element.

Sign up to request clarification or add additional context in comments.

Comments

1

As stated here:

You can refer to a template reference variable anywhere in the component's template.

Comments

1

It turns out, in newer versions of Angular (I'm using v18) you can't access template variables from the outside if they're inside an @if or a @for block. Indeed, this could be mentioned in the docs.

 <!-- won't work, you get 'Property `variable` does not exist on type SomeComponent'-->
{{variable.offsetHeight}}

@if (condition) {
<div #variable></div>
}

If you still want to use variables behind if or for statements, you should create a new viewChild variable in your TS file.

// with signals:
variable = viewChild<ElementRef<HTMLDivElement>>('variable');
// or with the older method:
@ViewChild('variable') variable: ElementRef<HTMLDivElement>;

And then in the HTML template:

{{variable().nativeElement?.scrollHeight  }}

Be sure to use ?. operator or something similar because variable().nativeElement might not exist if the element isn't rendered because of the if condition.

1 Comment

Seeing this behavior on v17 as well. Rather big migration pitfall to not even mention in docs!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.