Angular 2 中的 * ngIf 指令

NgIf是一个内置的模板指令,它根据传递给它的表达式是TRUE还是FALSE来添加或删除部分DOM:

1<div *ngIf="userHasPet">
2  {{ user.pet.name }}
3</div>

在上面的示例中,如果userHasPet为True,则div将包含在DOM中(它将位于页面上),如果userHasPet为False,则将从DOM中删除div(它将不在页面上)。

与 * ngFor一样,* 字符允许创建模板,并允许使用以下语法的快捷方式:template=ngIf userHasPet

您还可以将更复杂的表达式传递给* ngIf:

1<div *ngIf="user.name.length > 6 && user.name.length < 10">
2  Long name {{ user.name }}, but not too long!
3</div>

See So

Published At
Categories with 技术
Tagged with
comments powered by Disqus