2

I'm using Angular and Ionic and i trying to read a local variable in the template, but when I use *ngIf on an ion-item, even if true the data becomes unavailable. How to solve this? Or is it a Bug?

Please open my plucker and click on the button to see that the local variable p2 does not return because of the *ngIf ...

See in plunker code sample problem

Snippet on template

<ion-content class="has-header">
  <div padding style="text-align: center;">
    <h1>Ionic 2 Test</h1>
  </div>
  <ion-item >
    <p #p1>10</p>
  </ion-item>
  <ion-item *ngIf='visible'>
    <p #p2>20</p>
  </ion-item>
  <button block (click)='show(p1?.textContent,p2?.textContent);'> Click-me and you'll see that p2 does not recover </button>
  <ion-item>
    <p style='text-align: center;'>{{ backtemplate }}</p>
  </ion-item>
</ion-content>

Simple function on component

show(p1,p2){
  this.backtemplate = p1+' AND '+p2;

}
12
  • trying using double quotes, like so *ngIf="visible" Commented Apr 17, 2017 at 14:25
  • @Deepak unfortunately it makes no difference. Commented Apr 17, 2017 at 14:32
  • where are you toggling 'visible' property's value? Commented Apr 17, 2017 at 14:34
  • 1
    hmm.. tried without ? it is not even able to find p2..maybe the id #p2 is not being set due to angular's change detection? Commented Apr 17, 2017 at 14:34
  • 1
    *ngIf works as usual, if elements aren't referenced directly in html. ngIf seems to not work fine, when used with element references. One option is to use variables and bind them with p element or if you want to reference elements, you can try this: plnkr.co/edit/qXQAZMksHF1qzeW0zNrs?p=preview Commented Apr 17, 2017 at 16:05

1 Answer 1

2

Was testing all possibilities of codes and using [*ngIf]="visibile" instead of *ngIf="visible" solved the problem.

Now the problem is that i can't explain why it worked that way, sorry for that, but the code is working fine.

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

1 Comment

Actually it seems to have worked because *ngIf is not working anymore..It is same as removing ngIf..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.