gpt4 book ai didi

Angular 5 : *ngIf ="isValid;else other_content" VS *ngIf ="isValid" . *ngIf ="!isValid"

转载 作者:太空狗 更新时间:2023-10-29 19:26:49 25 4
gpt4 key购买 nike

这个问题是关于理解为什么一种技术比另一种更好。在 Angular 4/5 中,在模板中,您可以通过执行以下操作来实现相同的目的:

1) *ngIf-else 语法

<div *ngIf="isValid;else other_content">
content here ...
</div>

<ng-template #other_content>other content here...</ng-template>

2) *ngIf="isValid"*ngIf="!isValid"

<div *ngIf="isValid">
content here ...
</div>

<div *ngIf="!isValid">
other content here...
</div>

这两个语法是完全有效的,使用语法 1,你可以更进一步像 describe here ,但是是否有使用其中一个与另一个的任何性能/最佳实践建议?

最佳答案

两个 ngIf 指令被编译两次并导致两个绑定(bind)而不是一个。

如果表达式包含管道,这会变得更加困惑:

<div *ngIf="isValidAsync | async">...</div>
<div *ngIf="!(isValidAsync | async)">...</div>

将导致两个订阅。

ngIf else 模板正是为了解决这种情况而受支持的,应作为经验法则使用。

关于 Angular 5 : *ngIf ="isValid;else other_content" VS *ngIf ="isValid" . *ngIf ="!isValid",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48688279/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com