gpt4 book ai didi

javascript - Angular 为什么星号(*)

转载 作者:IT王子 更新时间:2023-10-29 02:49:18 26 4
gpt4 key购买 nike

在 Angular 文档中,* and template ,我们知道*ngIf、*ngSwitch、*ngFor可以扩展为ng-template标签。我的问题是:

我认为没有*ngIfngFor也可以被Angular引擎翻译和扩展为模板标签。

下面的代码

<hero-detail *ngIf="currentHero" [hero]="currentHero"></hero-detail>

会和

一样
<ng-template [ngIf]="currentHero">
<hero-detail [hero]="currentHero"></hero-detail>
</ng-template>

那么为什么还要在 Angular 中设计一个奇怪的符号星号 (*) 呢?

最佳答案

Asterisk 语法是更冗长的模板语法的语法糖,它的指令扩展到引擎盖下,您可以自由使用这些选项中的任何一个。

引自docs :

The asterisk is "syntactic sugar". It simplifies ngIf and ngFor for both the writer and the reader. Under the hood, Angular replaces the asterisk version with a more verbose form.

The next two ngIf examples are effectively the same and we may write in either style:

<!-- Examples (A) and (B) are the same -->

<!-- (A) *ngIf paragraph -->
<p *ngIf="condition">
Our heroes are true!
</p>

<!-- (B) [ngIf] with template -->
<template [ngIf]="condition">
<p>
Our heroes are true!
</p>
</template>

关于javascript - Angular 为什么星号(*),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40078189/

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