Call a Function from an *Ngif Angular 5

Say I have this code here

<div *ngIf="item">lorem ipsum</div>

Is there a way I can call a function if that *ngIf evaluates to true??

you know something like this..

<div *ngIf="(item) : callFunction() ? ...">lorem ipsum</div>

any help would be appreciated!

Thanks

4

3 Answers

Angular way would be:

<div *ngIf="name; then func(); else false">;</div>

But as *ngIf evaluates passed in logical expression, you can also do:

<div *ngIf="name?func():false">;</div>

Try like this

<div *ngIf="item ===true?callFunction():'otherStuff'">lorem ipsum</div>
0

You can try like this

Html

<div *ngIf="item; then callfunction; else nofunction"></div>
<ng-template #callfunction>
  {{call()}}
</ng-template>
<ng-template #nofunction>
 <!-- something else -->
</ng-template>

Ts

call(){
}

If you got better solution than this please post that to

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Marcus Vance

Marcus Vance

Cybersecurity & Digital Privacy Researcher

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.

Share this article
Twitter Facebook Pinterest