8

I am trying to define a template variable on an element and use its hidden attribute to identify whether the element is actually present in the DOM and then display another element based on that. But if there is a structural directive, template variable does not seem to return a value.

<hr class="divider" *ngIf="true" #divi>
<div *ngIf="showResendWelcomeEmailButton">
  <a *wpHasAnyPermission="[{'something': true}]" 
     #resendEmailBtn>
    Resend Welcome Email
  </a>
</div>
<div class="pull-right">
  <a #editAccountBtn>Edit Account Details</a>
</div>

rbtn: {{resendEmailBtn?.hidden}}
ebtn: {{editAccountBtn?.hidden}}
dline: {{divi?.hidden}}

Output is

rbtn:
ebtn: false
dline: 

As you can see both the template variables on elements containing attributes ngIf and wpHasAnyPermission are not returning an values.

What I want to eventually do is to use resendEmailBtn and editAccountBtn in ngIf of hr to decide displaying the divider.

What is the best way to solve this ? I want to avoid dealing with component code. Try to solve this with in HTML.

Alexander Abakumov
  • 13,617
  • 16
  • 88
  • 129
TechCrunch
  • 2,924
  • 5
  • 45
  • 79

1 Answers1

9

The variable is not available outside the element where *ngIf is applied.

<hr class="divider" *ngIf="false" #divi>

will be replace by

<template let-divi [ngIf]="false">
  <hr class="divider"  >
</template>

and divi will only be available within the <template> element.

You can add

@ViewChild('editAccountBtn') editAccountBtn:ElementRef;

to your components class, to make it available within the whole components template. It only has a value, when the queried element is added to the DOM. If it's within an *ngIf that evaluates to false the value will be null.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • I'm curious how `editAccountBtn` was accessible outside. What you mentioned is applicable only to elements with structural attributes ? What would be the best way to solve my problem ? – TechCrunch Jul 21 '17 at 22:27
  • I'm not sure if I asked the question clearly. What I wanted to do was `
    ` so that I can display the divider only when the button is present. I don't care about the variable of `hr` element.
    – TechCrunch Jul 21 '17 at 22:38
  • thanks for taking time to respond. Just to be clear, I don't have problem accessing editAccountBtn since it does not any structural directives on it. However, resendEmailBtn is not accessible. I tried to add ViewChild for that in the component but that did not make any difference.. – TechCrunch Jul 21 '17 at 23:38
  • Sorry, was a bit late already.. With `@ViewChild()` it also only is available when `*ngIf` is `true`, otherwise the content doesn't exist and the reference is null. Did you try with `true` – Günter Zöchbauer Jul 22 '17 at 07:12
  • `resendEmailBtn` is on a button that is true and I mean I can see the element getting displayed. – TechCrunch Jul 22 '17 at 22:14