typescript 从 *ngIf Angular 5 调用函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49642243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Call a function from an *ngIf Angular 5
提问by Smokey Dawson
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
谢谢
回答by Julius Dzidzevi?ius
Angular way would be:
角度方式将是:
<div *ngIf="name; then func(); else false">;</div>
<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="name?func():false">;</div>
回答by brk
Try like this
像这样尝试
<div *ngIf="item ===true?callFunction():'otherStuff'">lorem ipsum</div>
回答by Abinesh Joyel
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
如果您有比这更好的解决方案,请将其发布到