Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 696
Sasuke Uchiha Avatar asked Jun 05 '20 10:06

Sasuke Uchiha


People also ask

What does template reference variable contain?

A template reference variable is often a reference to a DOM element within a template. It can also refer to a directive (which contains a component), an element, TemplateRef, or a web component.

How do I access the template reference variable in component?

Usually, the reference variable can only be accessed inside the template. However, you can use ViewChild decorator to reference it inside your component. @ViewChild('firstNameInput') nameInputRef: ElementRef; After that, you can use this.

What is the use of template in Angular?

A template is a form of HTML that tells Angular how to render the component. Views are typically organized hierarchically, allowing you to modify or show and hide entire UI sections or pages as a unit. The template immediately associated with a component defines that component's host view.

What is the value type that will be stored in the Headertext template reference variable in this markup?

element. Is This Question Helpful?


2 Answers

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.

like image 194
ruth Avatar answered Sep 29 '22 12:09

ruth


As stated here:

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

like image 32
Mike S. Avatar answered Sep 29 '22 11:09

Mike S.