角2为什么星号(*) [英] angular 2 why asterisk (*)

查看:72
本文介绍了角2为什么星号(*)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在angular 2文档中, *和模板,我们知道* ngIf,* ngSwitch,* ngFor可以扩展为模板标记.我的问题是:

In angular 2 document, * and template, we know that the *ngIf, *ngSwitch, *ngFor can be expanded to template tag. My question is:

我认为不带*ngIfngFor也可以通过角度引擎翻译并扩展为模板标签. 那么,为什么还要在角度2中设计一个奇怪的符号星号(*)呢?

I think the ngIf or ngFor without * can also be translated and expanded to template tag by angular engine. So why bother designing a strange symbol asterisk(*) in angular 2?

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

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

推荐答案

星号语法是一种语法糖,可用于更多罗template的模板语法,该指令可扩展为高级功能,您可以自由使用这些选项中的任何一个.

Asterisk syntax is a syntatic sugar for more wordy template syntax which directive expands to under the hood, you are free to use any of these options.

文档的报价:

星号是语法糖".它简化了ngIf和ngFor 无论是作家还是读者.在引擎盖下,Angular取代了 星号版本,具有更详细的形式.

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.

接下来的两个ngIf示例实际上是相同的,我们可以用以下任何一种格式编写:

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>

这篇关于角2为什么星号(*)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆