简体   繁体   English

从* ngIf Angular 5调用函数

[英]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?? 如果* ngIf的计算结果是真的,有没有办法可以调用函数?

you know something like this.. 你知道这样的事情..

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

any help would be appreciated! 任何帮助,将不胜感激!

Thanks 谢谢

Angular way would be: 角度方式是:

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

But as *ngIf evaluates passed in logical expression, you can also do: 但是,当*ngIf评估传递的逻辑表达式时,您还可以执行以下操作:

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

试试这样吧

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

You can try like this 你可以这样试试

Html HTML

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

Ts TS

call(){
}

If you got better solution than this please post that to 如果你有比这更好的解决方案,请发布到

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM